memory allocation for ref and val types<p align=left><font face=Arial size=2></font> </p> <p>hi everyone. since i have not found enough information to decide how exactly types are allocated in memory, i am pleased to ask members of this online community. there are five questions overall on my mind regarding the matter of dynamic and in-line memory allocation.</p> <p align=left><br>two questions about reference types:<br>1. are class objects always allocated on the heap but their references are always allocated on the stack? (refer to msdn)<br>2. are class objects always allocated on the heap but their references might be allocated on the stack(if located inside value type) or on the heap(if located inside reference type)? (refer to c-sharpcorner.com for <span class=AuthorName>Matthew Cochran's article)</span></p><span class=AuthorName></span> <p align=left><br>three questions about value types:<br>1. are structures always allocated on the stack? (refer to msdn)<br>2. might structures be allocated on the stack(if located inside value type) or on the heap(if located inside reference type)? (refer to c-sharpcorner.com for <span class=AuthorName>Matthew Cochran's article)</span><br>3. do value types contain references when they are boxed?</p> <p align=left><br>you are more than welcome to anwer these questions, make corrections, and let me know where i am wrong. thank you.</p>© 2009 Microsoft Corporation. All rights reserved.Mon, 27 Oct 2008 07:36:36 Z22d16fde-b1e8-4c07-b881-486c9e4dfc08http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/22d16fde-b1e8-4c07-b881-486c9e4dfc08#22d16fde-b1e8-4c07-b881-486c9e4dfc08http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/22d16fde-b1e8-4c07-b881-486c9e4dfc08#22d16fde-b1e8-4c07-b881-486c9e4dfc08justvictorhttp://social.msdn.microsoft.com/Profile/en-US/?user=justvictormemory allocation for ref and val types<p align=left><font face=Arial size=2></font> </p> <p>hi everyone. since i have not found enough information to decide how exactly types are allocated in memory, i am pleased to ask members of this online community. there are five questions overall on my mind regarding the matter of dynamic and in-line memory allocation.</p> <p align=left><br>two questions about reference types:<br>1. are class objects always allocated on the heap but their references are always allocated on the stack? (refer to msdn)<br>2. are class objects always allocated on the heap but their references might be allocated on the stack(if located inside value type) or on the heap(if located inside reference type)? (refer to c-sharpcorner.com for <span class=AuthorName>Matthew Cochran's article)</span></p><span class=AuthorName></span> <p align=left><br>three questions about value types:<br>1. are structures always allocated on the stack? (refer to msdn)<br>2. might structures be allocated on the stack(if located inside value type) or on the heap(if located inside reference type)? (refer to c-sharpcorner.com for <span class=AuthorName>Matthew Cochran's article)</span><br>3. do value types contain references when they are boxed?</p> <p align=left><br>you are more than welcome to anwer these questions, make corrections, and let me know where i am wrong. thank you.</p>Sun, 25 May 2008 03:50:20 Z2008-05-26T14:42:16Zhttp://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/22d16fde-b1e8-4c07-b881-486c9e4dfc08#11a71cd2-1528-4abd-8b0c-695cfb059496http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/22d16fde-b1e8-4c07-b881-486c9e4dfc08#11a71cd2-1528-4abd-8b0c-695cfb059496Ron.Whittlehttp://social.msdn.microsoft.com/Profile/en-US/?user=Ron.Whittlememory allocation for ref and val types<p align=left><font face=Arial size=2>Reference types:</font></p> <p align=left> </p> <p align=left>Class objects are allocated on the heap. Since everything in the class object is on the heap, any references it has will also be on the heap. The two statements you have are both true, they just aren't talking about the same thing.</p> <p align=left> </p> <p align=left>Value types</p> <p align=left> </p> <p align=left>Structures are allocated on the stack, unless they are contained in a class, in which case they are in the class, which is on the heap. </p> <p align=left> </p> <p align=left>I can't answer how boxed value types are delt with, I can see it being handled both ways, but I suspect that the reference is allocated on the stack, which then references the location of the value type (which might be on the stack or in another object on the heap).</p> <p align=left> </p>Sun, 25 May 2008 06:46:24 Z2008-05-25T13:51:17Zhttp://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/22d16fde-b1e8-4c07-b881-486c9e4dfc08#9010e023-eb92-48aa-bf9f-8096fa30cc14http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/22d16fde-b1e8-4c07-b881-486c9e4dfc08#9010e023-eb92-48aa-bf9f-8096fa30cc14Marc Gravellhttp://social.msdn.microsoft.com/Profile/en-US/?user=Marc%20Gravellmemory allocation for ref and val types<p> <blockquote> <table width="85%"> <tbody> <tr> <td class=quoteTable> <table width="100%"> <tbody> <tr> <td class=txt4 valign=top width="100%">I can't answer how boxed value types are delt with,</td></tr></tbody></table></td></tr></tbody></table></blockquote> <p></p> <p align=left>Re the box - the box is created as a managed object on the heap that encapsulates a *copy* (blit) of the original value-type; it doens't reference the original location of the struct. This happily means that if the original was on the stack, then it still exists after the calling method has exited and torn down the stack.</p> <p align=left> </p> <p align=left>Marc</p> <p align=left><font face=Arial size=2></font> </p>Sun, 25 May 2008 08:20:35 Z2008-05-25T13:51:22Zhttp://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/22d16fde-b1e8-4c07-b881-486c9e4dfc08#f0f3893d-9863-4381-835a-fda60427b5fbhttp://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/22d16fde-b1e8-4c07-b881-486c9e4dfc08#f0f3893d-9863-4381-835a-fda60427b5fbjustvictorhttp://social.msdn.microsoft.com/Profile/en-US/?user=justvictormemory allocation for ref and val types<p align=left><font face=Arial size=2></font> </p> <p align=left><font face=Arial size=2>both replies served as answers to my questions. i appreciate.</font></p>Sun, 25 May 2008 14:02:17 Z2008-05-25T14:02:17Zhttp://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/22d16fde-b1e8-4c07-b881-486c9e4dfc08#783f0b5a-6528-484c-a620-0c50ba3d72d3http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/22d16fde-b1e8-4c07-b881-486c9e4dfc08#783f0b5a-6528-484c-a620-0c50ba3d72d3Sasha Goldshteinhttp://social.msdn.microsoft.com/Profile/en-US/?user=Sasha%20Goldshteinmemory allocation for ref and val typesA reference to a reference type <span style="font-style:italic">can</span> live on the stack.  If I have a string local variable in a method, then the string is allocated on the heap but the reference to the string lives on the stack.<br><br>It's possible for a value type to be allocated on the heap as part of a reference type (e.g. as part of an array).  Boxing is another example.<br><br>Note that there are some bizarre cases, e.g. you can allocate a &quot;kind of&quot; array on the stack (even though it's a reference type) using the unsafe <span style="font-style:italic">stackalloc</span> keyword.<br>Sun, 25 May 2008 19:15:16 Z2008-05-26T14:42:16Zhttp://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/22d16fde-b1e8-4c07-b881-486c9e4dfc08#b1a2cb5a-7a42-474f-8883-6ec43c0254e9http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/22d16fde-b1e8-4c07-b881-486c9e4dfc08#b1a2cb5a-7a42-474f-8883-6ec43c0254e9sumannethttp://social.msdn.microsoft.com/Profile/en-US/?user=sumannetmemory allocation for ref and val types hi ,<br><br><br>value types  are always allocated on stack and reference types  are always on heap.<br><br>if value  tpye is inline to reference type then it still allocated on stack which is inline to that heap.<br><br>actually when we create value type then reference type also created without our knowledge which encapsulate that value type.Mon, 27 Oct 2008 07:36:36 Z2008-10-27T07:36:36Z