views:

472

answers:

4

Why does C# prefer stack to store value types ? What is the primary reason behind this design ? Won't stack R/W operations would be processing-savy ?

or may be you can justify why not others ?

A: 

Stack operation or Heap operation, they both will be same as you are accessing a memory address which are in two different locations.

Value types are small, int, byte etc, they are small in size and they are referenced very frequently in terms of mathematical computations. Since they are very small in size, 4 to 16 bytes max, (you should not use more then 16 bytes in value type for best performance), alloating such small space on heap and deallocating, garbage collection etc would be very costly.

Every method we enter, on an average we define 10 local value types to use it internally, that will be very costly on heap as reference types.

Stack can grow and shrink easily (not the stack size, but the stack portion used for current Method !!) as the valuetypes are just addressed as offset from stack pointer, and their allocation and deallocation is easy as its simple increment and decrememnt on stackpointer by total size of all valuetypes used.

Where else in reference type, each reference object has its own allocation and sizing, plus CLR has to maintain table of objects which is sort of like index of actual pointers in memory in order to avoid buffer overflows. So one object you use (reference type) actually has two storage, one index entry in CLR's reference table, and actual memory space. Thats why its easy and fast to store value types on stack.

Akash Kava
The stack size can't really grow or shrink once it is set for a thread. Instead, the stack pointer is moved up or down depending on what is present on the stack.Also, boxing isn't what you describe. Boxing is taking a value type and allocating a handle on the GC heap. This can cause a collection and so on average it is not cheap. Unboxing is faster since it copies the value on the heap to the stack without a memory alloc or any GC pressure added.
codekaizen
I have clarified, ofcourse stack size doesnt grow or shrink but the part of stack portion used for current method's utilization, which is size of all local variables can grow or shrink. Because this stack is not pure stack it has some reserved spaces for current method.
Akash Kava
You're still wrong about the boxing bit, though.
codekaizen
+3  A: 

As @Akash indicates, it mostly has to do with memory. During the design of the CLR, it was noted (my guess was from the experience with Java) that representing small, primitive types as objects with handles subjected to the garbage collector caused a lot of tracking overhead. So the designers wanted a "lightweight" object which didn't need to be tracked.

There is no specific requirement in the CLI specification for primitives to be stack allocated; it's an artifact of the implementation on the machine. The essential bit is that the runtime knows where the instances are due to the construction of well-defined patterns of memory (called frames) rather than in the GC's index of allocated objects. On x86 (and similar) machines, this can be done efficiently using the stack.

codekaizen
+7  A: 

Eric Lippert discusses this here; firstly, it is incorrect that "value types are stored on the stack". They sometimes are, but not as:

  • fields on a class
  • captured variables
  • variables in an iterator block

When they can be stored on the stack it is a convenient way of modelling their lifetime, but it isn't required to store them on the stack. You could write a compiler+CLI that doesn't have a stack, for example.

Marc Gravell
+4  A: 

C# doesn't store anything on the stack. C# is a programming language. Therefore, a more correct version of your question is why does the Microsoft C# compiler emit CIL instructions to allocate value types on the stack?

Well, first, it only does sometimes. The following do not go on the stack:

  1. Value types that are fields in a class
  2. Boxed value types
  3. Local value types that are outer variables of anonymous methods
  4. Local value types that are outer variables of iterator blocks

Second, when it's possible it's done because it's efficient. Basically in the CLR memory model, on the stack deallocation is very cheap relative compared to deallocation on the heap. With locals of value types, you can be sure that no one else except the local will reference the memory so you can get away with using the stack instead of the heap. For details, see Eric Lippert.

Finally, what makes value types special is that they have value-type semantics (copy by value), not that they are sometimes allocated on the stack. There is no requirement in the C# specification that the compiler emit instructions to allocate value types on the stack. What the C# specification does require is that value-types have value-type semantics.

Jason