Stack and heap in c sharp
Possible Duplicate:
Why are structs stored on the stack while classes get stored on the heap(.NET)?
Can anyone tell me that how the allocation of memory is done that which object is to be stored in stack and which to be in heap portion of the memory?
3 rules of thumb:
In the Microsoft implementation of the C# compiler and CLR, value types are stored on the stack when the value is a temporary value, local variable or formal parameter, that is neither a closed-over outer variable of an anonymous method nor in an iterator block.
Of course, why store stuff on the stack if you don't need to? Some local variables of value type never get on the stack at all; they stay in registers for their entire lifetimes.
Other values of value types are stored on the heap - boxed value types, value-typed fields on a reference type, and so on.
Value types can of course be stored on neither the stack, nor registers, nor the managed heap; they could be stored in unmanaged memory using some completely other memory manager not under control of the CLR.
(And of course note that using "the" in "the stack" is subtly misleading; there can be many stacks in a process. There need not be just one.)
All this is an implementation detail and subject to change without notice.
Also, obviously stuff allocated with the stack alloc declaration is allocated on the stack.
For more information on this topic see my articles on it:
http://blogs.msdn.com/b/ericlippert/archive/2009/04/27/the-stack-is-an-implementation-detail.aspx
http://blogs.msdn.com/b/ericlippert/archive/2009/05/04/the-stack-is-an-implementation-detail-part-two.aspx
Why do you care? The runtime manages all these details for you so that you don't have to worry about it. Are you just curious, or is this leading to some larger question?
链接地址: http://www.djcxy.com/p/82712.html上一篇: C嵌入式系统中的内存管理
下一篇: 堆叠和堆在C锐利