locked
.Net structures corresponding to C# value types, are they stored on the managed heap? RRS feed

  • Question

  • Hi,

    I have read the following statements about C# value types: -

    Value types inherit from the System.ValueType class, which in turn, inherits from System.Object.
    Value types are stored on the stack in memory.
    Value type objects have two representations: an unboxed form and a boxed form.
    When the CLR boxes a value type, it wraps the value inside a System.Object and stores it on the managed heap.
    There are .Net inbuilt structures that correspond to C#'s built-in value types. e.g. - System.Int32 corresponds to int

    My query is: - if System.Int32 is a structure, then how can it be stored on the heap instead of stack? Is System.Int32 a reference type?

     

    Tuesday, August 9, 2011 5:45 PM

Answers

  • Int32 is a value type. value types, instantiated on their own will reside in the stack. However, when value types are members of a reference type, they are part of the memory allocated for the reference type, i.e. by virtue of being a member of a reference type, they reside in reference type.

    Another scenario when a value type will reside in heap is by means of boxing. int is a value type, but when you write the code

    int i = 3;
    object o = i;
    


    The integer is boxed and is moved to the heap. The process involves

    1. memory allocated in heap

    2. The value type is copied to the heap. An additional pointer to this heap memory is created and is accessible through variable o.

    • Marked as answer by ajayubbott Wednesday, August 10, 2011 2:01 AM
    Tuesday, August 9, 2011 6:25 PM

All replies

  • "Value types are stored on the stack in memory."
    That's a common misconception. Value types can be stored in both stack memory and heap memory. Consider this example:
    class foo {
     public int bar;
    }
    

    Obviously, foo is a class so it is stored in heap memory. The int value bar, a member of class foo, it's stored in the instance of class foo, so it is stored in heap memory too.
    When an integer gets boxed the resulting object looks very similar to the above class, so the boxed version of int, a value type, is stored in the heap memory too.

    Tuesday, August 9, 2011 6:19 PM
  • No, Int32 is a struct and therefore a value type.  It's stored on the stack.  It's value is stored inline, nothing pointing to the heap in any way. That is, unless it gets boxed in a reference type in which case it's moved to the heap and the reference type object that the value type is now wrapped in is referenced by a pointer that lives on the stack.  But the value of the value type variable itself is not on the stack anymore.

    I can see your confusion when you think about the fact that System.Int32 derives from ValueType and Object which are reference types.  You would then logically think System.Int32 should be a ref type. But Int32 (and all value types) must inherit from System.object so they can be treated as objects (because everything is an object in the .NET world).     


    Tom Overton
    Tuesday, August 9, 2011 6:24 PM
  • Int32 is a value type. value types, instantiated on their own will reside in the stack. However, when value types are members of a reference type, they are part of the memory allocated for the reference type, i.e. by virtue of being a member of a reference type, they reside in reference type.

    Another scenario when a value type will reside in heap is by means of boxing. int is a value type, but when you write the code

    int i = 3;
    object o = i;
    


    The integer is boxed and is moved to the heap. The process involves

    1. memory allocated in heap

    2. The value type is copied to the heap. An additional pointer to this heap memory is created and is accessible through variable o.

    • Marked as answer by ajayubbott Wednesday, August 10, 2011 2:01 AM
    Tuesday, August 9, 2011 6:25 PM
  • Many thanks for the answers, Guys :-)
    Wednesday, August 10, 2011 2:01 AM