views:

150

answers:

3

In IL, you can define local variables using the .locals directive. Where are these variables stored, stack or heap?

+3  A: 

If the object is not a value type, it is allocated on the heap and a reference to it is stored on the stack. Otherwise, it is directly allocated on the stack.

Justin R.
+7  A: 

On the stack with parameters. ..BUT .....
1) for reference types, only the reference is stored on the stack not the object its referring to. The actual object is stored on the heap.
2) for value types, the actual value is stored on the stack.

Now when the execution flow in the method reaches the closing brace the value type data on the stack is destroyed there and then , while the reference type objects on the heap (whose references were here on this method's stack ) are handed over to the garbage collection system for collection at an appropriate time decided by the garbage collector itself.

mumtaz
This is very misleading too for the same reasons as in the marked answer. Plus there is most definitely no destroying going on. Neither in the IL nor the machine code. The stack location is simply abandoned and gets eventually overwritten by another activation stack frame. Even the notion that reference types are on the heap is not accurate. Interned strings are actually stored in the loader heap, same place where static variables are kept. Details, details.
Hans Passant
@Hans: thanks for the details... appreciated your explanation.
mumtaz
+5  A: 

It is very much an implementation detail of the JIT compiler. It will try very hard to store local variables in a CPU register, very efficient. The stack is the usual backing store, in case there aren't enough registers available to store all the local variables.

Big difference between the x86 and x64 jitters for example. x64 has a lot more registers available. This also applies to the arguments passed to a method. x86 allows 2 passed in a CPU register, x64 allows 4. Plus whatever can be stored in the FPU stack or XMM registers. So, there are really four distinct places a local variable can be stored.

Hans Passant
+1, there's a lot of confusion about locals and parameters when it comes to IL. IL works with an operation stack, but this does not directly correspond to a 'call stack', such as that generated by a C compiler with the 'cdecl' or 'stdcall' calling convention. Both arguments and locals are treated as numbered slots; the only area where there is a similarity to a call stack is when the parameters to a method call are popped from the operation stack for loading into the argument slots for the method call.
Dan Bryant