Microsoft Developer Network > Domovská stránka fór > Visual C# General > memory allocation for ref and val types
Odeslat dotazOdeslat dotaz
 

Odpovědětmemory allocation for ref and val types

  • 25. května 2008 3:50justvictor Uživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     

     

    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.

Odpovědi

  • 25. května 2008 6:46Ron.Whittle Uživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     Odpovědět

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

     

  • 25. května 2008 8:20Marc GravellMVPUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     Odpovědět

    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

     

  • 25. května 2008 19:15Sasha GoldshteinMVPUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     Odpovědět
    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.

Všechny reakce

  • 25. května 2008 6:46Ron.Whittle Uživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     Odpovědět

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

     

  • 25. května 2008 8:20Marc GravellMVPUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     Odpovědět

    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

     

  • 25. května 2008 14:02justvictor Uživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     

     

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

  • 25. května 2008 19:15Sasha GoldshteinMVPUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     Odpovědět
    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.
  • 27. října 2008 7:36sumannet Uživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaileUživatelské medaile
     
    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.