Visual C# Developer Center > Visual C# Forums > Visual C# General > memory allocation for ref and val types
Ask a questionAsk a question
 

Answermemory allocation for ref and val types

  • Sunday, May 25, 2008 3:50 AMjustvictor Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

     

    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.


    two questions about reference types:
    1. are class objects always allocated on the heap but their references are always allocated on the stack? (refer to msdn)
    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 Matthew Cochran's article)


    three questions about value types:
    1. are structures always allocated on the stack? (refer to msdn)
    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 Matthew Cochran's article)
    3. do value types contain references when they are boxed?


    you are more than welcome to anwer these questions, make corrections, and let me know where i am wrong. thank you.

Answers

  • Sunday, May 25, 2008 6:46 AMRon.Whittle Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    Reference types:

     

    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.

     

    Value types

     

    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.

     

    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).

     

  • Sunday, May 25, 2008 8:20 AMMarc GravellMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    I can't answer how boxed value types are delt with,

    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.

     

    Marc

     

  • Sunday, May 25, 2008 7:15 PMSasha GoldshteinMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    A reference to a reference type can 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.

    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.

    Note that there are some bizarre cases, e.g. you can allocate a "kind of" array on the stack (even though it's a reference type) using the unsafe stackalloc keyword.

All Replies

  • Sunday, May 25, 2008 6:46 AMRon.Whittle Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    Reference types:

     

    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.

     

    Value types

     

    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.

     

    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).

     

  • Sunday, May 25, 2008 8:20 AMMarc GravellMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    I can't answer how boxed value types are delt with,

    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.

     

    Marc

     

  • Sunday, May 25, 2008 2:02 PMjustvictor Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

     

    both replies served as answers to my questions. i appreciate.

  • Sunday, May 25, 2008 7:15 PMSasha GoldshteinMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    A reference to a reference type can 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.

    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.

    Note that there are some bizarre cases, e.g. you can allocate a "kind of" array on the stack (even though it's a reference type) using the unsafe stackalloc keyword.
  • Monday, October 27, 2008 7:36 AMsumannet Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    hi ,


    value types  are always allocated on stack and reference types  are always on heap.

    if value  tpye is inline to reference type then it still allocated on stack which is inline to that heap.

    actually when we create value type then reference type also created without our knowledge which encapsulate that value type.