Click here to Skip to main content
13,088,439 members (57,245 online)
Rate this:
Please Sign up or sign in to 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 11-Feb-13 17:22pm
Sergey Alexandrovich Kryukov 11-Feb-13 23:42pm
Who told you so? :-)
Rate this: bad
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;
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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

You can read Eric Lippert's post -[^]

Don't forget to mark it as answer if it helped you. Good luck!!
Rate this: bad
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.


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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.170813.1 | Last Updated 11 Feb 2013
Copyright © CodeProject, 1999-2017
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