Click here to Skip to main content
15,913,027 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
A value type is stored in a stack
A reference type is allocated memory on a heap.

A reference type has value types, eg class can have basic int properties or structs and structs can have a class object.
Then how does the class/struct get stored in the memory.
Does the struct gets memory allocation in stack or heap and where is the class contained in the struct goes.
Similarly a class having the value type is stored in heap however the where are the members of the class are stored like int variables etc.
Posted
Comments
Sergey Alexandrovich Kryukov 11-Feb-13 23:42pm    
Who told you so? :-)
—SA

Thats wrong. Not all value types are saved on the stack.

Many developers believe that reference types are stored on the heap while value types are always stored on the stack – this is not entirely true.

First it’s more of an implementation detail of the actual runtime and not a language requirement but more importantly it’s not possible – consider a class (reference type) which has a integer member (value type), the class is stored on the heap and so are it’s members including the value type since its data is copied “by-value”.

C#
class MyClass
{
    // stored in heap
    int a = 5;
}
 
Share this answer
 
Comments
Sakshi Smriti 11-Feb-13 23:37pm    
Thanks for your reply, can anyone post a link where I can find a detail reading related to it.
You can read Eric Lippert's post - http://blogs.msdn.com/b/ericlippert/archive/2009/04/27/the-stack-is-an-implementation-detail.aspx[^]

Don't forget to mark it as answer if it helped you. Good luck!!
 
Share this answer
 
No. This is not that simple. Values type objects are not stored just on stack. It depends on where they are used. They are stored on 1) stack; 2) memory reserved for static members, 3) on heap, when are used as a members of reference types (classes).

When a class is instantiated, it also not so simple. All reference variables/members take some place in memory. Suppose you have a local variable of a reference type. It is allocated on stack. Then you instantiate it, or assign already instantiated variable to it. Nothing happens to the reference itself — it remains on stack. But the value of the reference starts referencing some memory on stack. In second case, when you assign an already instantiated reference, you got two references referencing the same object on heap.

Consider references as "managed pointers" (C++/CLI terminology). They are not "real" pointers (this is not a place to discuss how managed system works in detail), but they are like pointers in terms of storage: the pointer "points" to some object (in case of .NET this is always the managed heap; I don't discuss unsafe pointers here which use the pointer syntax), but the pointer itself takes up some memory. Where? Again: on heap, on stack, in the "static" memory area.

In all cases, you should consider all the chain: what contains what.

—SA
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900