Microsoft Developer Network > 포럼 홈 > Visual C# General > memory allocation for ref and val types
질문하기질문하기
 

답변됨memory allocation for ref and val types

  • 2008년 5월 25일 일요일 오전 3:50justvictor 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     

     

    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.

답변

  • 2008년 5월 25일 일요일 오전 6:46Ron.Whittle 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     답변됨

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

     

  • 2008년 5월 25일 일요일 오전 8:20Marc GravellMVP사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     답변됨

    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

     

  • 2008년 5월 25일 일요일 오후 7:15Sasha GoldshteinMVP사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     답변됨
    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.

모든 응답

  • 2008년 5월 25일 일요일 오전 6:46Ron.Whittle 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     답변됨

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

     

  • 2008년 5월 25일 일요일 오전 8:20Marc GravellMVP사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     답변됨

    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

     

  • 2008년 5월 25일 일요일 오후 2:02justvictor 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     

     

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

  • 2008년 5월 25일 일요일 오후 7:15Sasha GoldshteinMVP사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     답변됨
    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.
  • 2008년 10월 27일 월요일 오전 7:36sumannet 사용자 메달사용자 메달사용자 메달사용자 메달사용자 메달
     
    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.