0

Possible Duplicate:
Stack & heap understanding question

I was told in Is using var actually slow? If so, why? that value types are stored on the stack and class types are stored in the heap.

class MyClass
{
  private int myInt;
  private int[] myInts;
}

Where is MyClass.myInt stored? MyClass is a class/reference type, so it's on the heap. myInt is a value type, so is it stored on some kind of "inside stack"?

Also, what about the array? Is it a value or a class?

Community
  • 1
  • 1
PRASHANT P
  • 1,527
  • 1
  • 16
  • 28
  • 5
    This is an exact duplicate of a thousand other somethings. I'll find one eventually. Until then, read http://blogs.msdn.com/b/ericlippert/archive/2010/09/30/the-truth-about-value-types.aspx – Anthony Pegram May 13 '11 at 19:25
  • 2
    I suggest you read Eric Lipperts blog it has many details on such issues. And he would tell you that saying that reference types always go on the heap and value types on the stack is not quite right. – Oded May 13 '11 at 19:26
  • 1
    var is neither slow nor fast! var is just syntactic sugar, that's determined at compile time. – alex May 13 '11 at 19:26
  • 2
    In 99 cases out of 100 you don't need to know. You only need to understand the difference in behavior between value types and reference types. – Albin Sunnanbo May 13 '11 at 19:29
  • Where were you told that value types are put on the stack? It doesn't say so in the referenced question as far as I can tell... – Jon Skeet May 13 '11 at 19:30
  • And who told you that using var was slow? You have a lot of misinformation in such a small space. – Bob G May 13 '11 at 19:32
  • @Jon Skeet, in this answer: http://stackoverflow.com/questions/5995876/why-var-is-slow/5995925#5995925 – Anthony Pegram May 13 '11 at 19:32
  • Your understanding that value types are stored on stack isn't true. In fact, possibility of even a local variable (which is a value type) to get stored on stack is actually very very low as stack is NOT the only memory area where local variables get stored. [Here](http://stackoverflow.com/questions/14022680/any-tool-to-see-where-variables-or-stored-while-net-program-getting-executed) is how. – RBT Feb 16 '17 at 01:26

4 Answers4

2

value types are put into stack and class types get put into heap

Only half of that is true, specifically the second half. Reference types are stored in the heap, that is always true.

The first part is not true, or rather only true sometimes. Value types that are local variables are stored on the stack, but value types that are member of a class or struct are stored inside that type. Value types that are boxed are stored inside an object on the heap.

So, an instance of your MyClass will contain an integer and a reference, and the instance of the class will always be stored on the heap.

The array is a separate object, and will also always be stored on the heap. If there actually is an array, that is. Until you have created an array and and assigned to myInts, it's null and there is no array.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • `Value types that are local variables are stored on the stack` - This understanding isn't that black and white. In fact, possibility of a local variable (which is a value type) to get stored on stack is actually very very low as stack is NOT the only memory area where local variables get stored. [Here](http://stackoverflow.com/questions/14022680/any-tool-to-see-where-variables-or-stored-while-net-program-getting-executed) is how. – RBT Feb 16 '17 at 01:13
0

Whatever is inside a reference type will get stored on the heap. The array will probably be stored in non-adjacent place to ''MyClass'' because it's size cannot be determined at compile time. If you're so confused with what goes where, any optimization is not your concern right now, you'll probably only make it worse, sorry.

Pasi Savolainen
  • 2,460
  • 1
  • 22
  • 35
0

Value types declared as local variables in a method are placed on the stack. When a value type is declared in a class, it is part of the data of that class, which is put on the heap. So with your class instances, myInt will be in the heap. When you declare any array, it is a class, so it gets it own place on the heap and your myInts value will be a reference to that object on the heap.

Mike Dour
  • 3,616
  • 2
  • 22
  • 24
  • 1
    Value type variables declared as local variables can still end up on the heap, if they're captured by anonymous functions, part of iterator blocks, or async methods. – Jon Skeet May 13 '11 at 19:31
  • 1
    Thanks Jon. Good to know. I guess it should also be mentioned that if you box a value type (set it as the value of a variable of type object), it will also be stored on the heap. – Mike Dour May 13 '11 at 19:34
0

They are meaning often the variables in the functions when talking about references and value. Your Class code exits in the TEXT part of memory. When you are creating an object (instantiating it) it (The object and all of the member variables) will be in Heap. But while passing to functions a reference to the object address at heap could be find on Stack.

Govan
  • 2,079
  • 4
  • 31
  • 53