How is an array of value types stored in .NET object heap?
In .NET, Value type object such as int is stored in memory.
Reference type object requires separate allocations of memory for the reference an开发者_StackOverflow中文版d 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?
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.
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.
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:
By wrapping it inside a regular object.
By boxing it.
By wrapping it inside an array object.
(There might be more ways but I don't think I've missed any.)
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.
精彩评论