Microsoft Developer Network > Forenhomepage > Visual C# General > memory allocation for ref and val types
Stellen Sie eine FrageStellen Sie eine Frage
 

Beantwortetmemory allocation for ref and val types

  • Sonntag, 25. Mai 2008 03:50justvictor TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     

     

    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.

Antworten

  • Sonntag, 25. Mai 2008 06:46Ron.Whittle TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     Beantwortet

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

     

  • Sonntag, 25. Mai 2008 08:20Marc GravellMVPTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     Beantwortet

    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

     

  • Sonntag, 25. Mai 2008 19:15Sasha GoldshteinMVPTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     Beantwortet
    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.

Alle Antworten

  • Sonntag, 25. Mai 2008 06:46Ron.Whittle TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     Beantwortet

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

     

  • Sonntag, 25. Mai 2008 08:20Marc GravellMVPTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     Beantwortet

    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

     

  • Sonntag, 25. Mai 2008 14:02justvictor TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     

     

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

  • Sonntag, 25. Mai 2008 19:15Sasha GoldshteinMVPTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     Beantwortet
    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.
  • Montag, 27. Oktober 2008 07:36sumannet TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     
    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.