Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are structs stored on the stack while classes get stored on the heap(.NET)?

Tags:

c#

.net

I know that one of the differences between classes and structs is that struct instances get stored on stack and class instances(objects) are stored on the heap.

Since classes and structs are very similar. Does anybody know the difference for this particular distinction?

like image 208
ichiban Avatar asked May 02 '09 18:05

ichiban


People also ask

Why struct is stored on the stack?

Most importantly, a struct unlike a class, is a value type. So, while instances of a class are stored in the heap, instances of a struct are stored in the stack. When an instance of a struct is passed to a method, it is always passed by value.

Are structs stored on the stack or heap?

Structs are allocated on the stack, if a local function variable, or on the heap as part of a class if a class member.

Why is class stored in heap?

Heap space is used for the dynamic memory allocation of Java objects and JRE classes at runtime. New objects are always created in heap space, and the references to these objects are stored in stack memory. These objects have global access and we can access them from anywhere in the application.

Why do we use heap instead of stack?

Stack accesses local variables only while Heap allows you to access variables globally. Stack variables can't be resized whereas Heap variables can be resized. Stack memory is allocated in a contiguous block whereas Heap memory is allocated in any random order.


2 Answers

(edited to cover points in comments)

To emphasise: there are differences and similarities between value-types and reference-types, but those differences have nothing to do with stack vs heap, and everything to do with copy-semantics vs reference-semantics. In particular, if we do:

Foo first = new Foo { Bar = 123 }; Foo second = first; 

Then are "first" and "second" talking about the same copy of Foo? or different copies? It just so happens that the stack is a convenient and efficient way of handling value-types as variables. But that is an implementation detail.

(end edit)

Re the whole "value types go on the stack" thing... - value types don't always go on the stack;

  • if they are fields on a class
  • if they are boxed
  • if they are "captured variables"
  • if they are in an iterator block

then they go on the heap (the last two are actually just exotic examples of the first)

i.e.

class Foo {     int i; // on the heap }  static void Foo() {     int i = 0; // on the heap due to capture     // ...     Action act = delegate {Console.WriteLine(i);}; }  static IEnumerable<int> Foo() {     int i = 0; // on the heap to do iterator block     //     yield return i; } 

Additionally, Eric Lippert (as already noted) has an excellent blog entry on this subject

like image 77
Marc Gravell Avatar answered Oct 04 '22 18:10

Marc Gravell


It's useful in practice to be able to allocate memory on the stack for some purposes, since those allocations are very fast.

However, it's worth noting that there's no fundamental guarantee that all structs will be placed on the stack. Eric Lippert recently wrote an interesting blog entry on this topic.

like image 21
mqp Avatar answered Oct 04 '22 18:10

mqp