Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: .NET3.5
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 11-Feb-13 18:22pm
Comments
Sergey Alexandrovich Kryukov at 11-Feb-13 23:42pm
   
Who told you so? :-)
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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”.
 
class MyClass
{
    // stored in heap
    int a = 5;
}
  Permalink  
Comments
Sakshi Smriti at 11-Feb-13 23:37pm
   
Thanks for your reply, can anyone post a link where I can find a detail reading related to it.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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!!
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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
  Permalink  
v3

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



Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 11 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100