1

Firstly, I'm currently working in C# and I've been reading up on memory management. So far, I've read through some great answers on stack overflow explaining the difference between stack memory and the managed heap memory. The majority of the answers state that by declaring:
int x = 5, you're allocating enough memory for the type of x within the stack memory.

I understand how this works as well as the scope of it, however when I read the explanation of heap memory, it confused me.

If you're saying int x = 5, since int is an alias of System.Int32, wouldn't x technically be a pointer to a new instance of the System.Int32 struct? And if so, wouldn't it then be stored in the heap memory since that's used for instance objects.

In this tutorial, it states (for the line class1 cls1 = new class1()):

... creates a pointer on the stack and the actual object is stored in a different type of memory location called ‘Heap’.

By this logic, isn't everything stored on the heap and only pointers stored on the stack? Examples being new instances of System.String, System.Int64, System.Boolean, System.Decimal etc.

I thought I understood it, however clearly I don't, so I would appreciate someone explaining whether the stack is only for pointers or which part I've misinterpreted. Thanks in advance.

18
  • 1
    System.Int32 is a struct, not a class. So, as a local, it will likely get allocated on the stack. Commented Sep 9, 2016 at 14:38
  • 1
    Keep in mind that the .NET CLR is changing, and the lines between "heap allocated" and "stack allocated" are continuing to get fuzzier. Commented Sep 9, 2016 at 14:40
  • 5
    The Truth About Value Types Commented Sep 9, 2016 at 14:44
  • 1
    The tutorial is wrong and incomplete. Commented Sep 9, 2016 at 14:44
  • 3
    @Nathangrad structs cannot be null in C#. Structs are value types. There is no reference to be null. The C# compiler will probably error with "use of uninitialized variable" but that's just the compiler trying to be helpful. Commented Sep 9, 2016 at 14:50

2 Answers 2

3

You can use the following rule: if it's a struct (including primitive types) then it's allocated where it's declared, otherwise a pointer to an object in heap is allocated.

Possible locations are:

  1. For local variables it's a stack. Note that physically values can be stored in CPU registers rather than in stack.
  2. For class fields it's inside of contiguous chunk of memory allocated in heap for an instance of the class.
  3. For static fields it's allocated in loader heap as a part of type metadata (сorrect me if I am wrong).

Warning: this is just basic, non-comprehensive explanation to have a basic understanding of what's going on. The reality is more complicated. Local variables can be hoisted and moved to heap, optimizer can eliminate them altogether, etc...

Sign up to request clarification or add additional context in comments.

4 Comments

For unhoisted local variables, it may be the stack. See the Eric Lippert post linked to in the comments if you want to be precise.
Yes, hoisted variables becomes fields of a display (closure) class
And the stack may not even be used even if the variable isn't hoisted. It may strictly use the register, or it may be optimized out entirely.
I've just had to google like 80% of the technical terms you've used in the answer, but I think I understand. Thanks!
-1

You may want to check Classes and structs (MSDN) to understand what is stored where and how:

int x = 1; // 32 bits holding an integer in the stack
System.Object bo = x; // 32+some more bits are on the heap to hold the "boxed" (wrapped to be kept on the heap) integer value
System.Object ho = new Object(); // some bits are created on the heap right from the start

In simple words there are two types of objects: classes and structs. The classes (reference types) are meant to be stored on the heap and have a pointer to them while structs are meant to be stored in the stack (the structs can be relocated to the heap though with a little overhead of wrapping("boxing") them).

If you really need/want to understand how CLR works in general, consider reading "CLR via C#" (Richter).

8 Comments

And then you can read ECMA specification if you have lots of free time.
If you wanted to add more, you should edit your answer. There shouldn't ever be a need to directly comment on your own (question/answer)
It's more complicated than just "value types are stored on the stack and reference types are stored on the heap."
classes [...] have a pointer to them in the stack The pointer isn't necessarily on the stack. It may be on the stack, which applies to any value, as Joshua also pointed out also makes the second half of that same sentence wrong.
As I (and Joshua) said before, the rest of the sentence is still wrong.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.