7

In .NET, Value type object such as int is stored in memory.

Reference type object requires separate allocations of memory for the reference and object, and the object is stored in .NET object heap.

And Array is created in the heap, so how an array of value types such as int[] stored in the heap? Does it mean value type object can be stored in the heap without boxing?

2
  • I would say that everything is stored in "memory" (well... except perhaps constants that aren't really stored and variables that are optimized out as useless or as constants). You can only speak of the "type" of memory (register/RAM/disk, speaking only of the directly accessible memory, so skipping the cache) or of its organization (stack, heap, ???) :-) You probably meant "stack memory", but that would have been wrong, as you discovered. Commented Feb 23, 2011 at 7:21
  • 'Value type object such as int is stored in memory' sounds a little misleading to me. The heap - of course - is just a kind of memory as well.. ? Commented Feb 23, 2011 at 7:24

4 Answers 4

6

Yes, you are right. I suggest you read this:

https://ericlippert.com/2010/09/30/the-truth-about-value-types/

It's very very good, and it explains nearly everything you'll ever want to know.

Sign up to request clarification or add additional context in comments.

1 Comment

Is that the case then, almost all time, all value types will store on HEAP only as - all instance variable will be on HEAP ..., Is this correct understanding ?
6

Yes, an array is one way in which a value type value can be stored on the heap without boxing. Another is just having it in a normal class:

public class Foo
{
    int value1;
    string name;
    // etc
}

All the variables associated with an instance of Foo are stored on the heap. The value of value1 is just the int, whereas the value of name is a string reference.

This is why the claim that "value types are stored on the stack, reference types are stored on the heap" is so obviously incorrect.

However, as Eric Lippert is rightly fond of pointing out, the stack/heap distinction is an implementation detail. For example, a future version of the CLR could store some objects on the stack, if it could work out that they wouldn't be needed after the method terminated.

Comments

0

Yes, it means that no boxing is done for reach element, because the entire array as a whole is "boxed" inside an Array object (although that's not what it's called).

There's really no requirement that says a value type has to be boxed before being placed on the heap. You can place a value type on the heap in three ways:

  1. By wrapping it inside a regular object.

  2. By boxing it.

  3. By wrapping it inside an array object.

(There might be more ways but I don't think I've missed any.)

Comments

0

Just think of it this way, the object location in memory is defined by what kind of type it is and where it was declared. If the object is a value type, its value is stored where you declared the variable. If the object is a reference type, its reference is stored where you declared the variable while the actual object instance exists on the heap.

When you declare a local variable, you are declaring the variable on the stack. Therefore a value type's value will be on the stack. A reference type's reference will be on the stack, and the object instance is still on the heap.

If you declare an instance variable within a class (a reference type), you are effectively declaring the instance variables in the heap. A value type's value will be in the heap (in the object instance). A reference type's reference will also be in the heap (in the object instance), the object instance will be elsewhere in the heap.

If you declare an instance variable within a struct (a value type), where it resides depends on where the underlying struct was declared.

In the case of an array of int int[], arrays are reference types and you can think of the int values declared as "fields" to that type so your integers are effectively in the heap.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.