How is an array of value types stored in a heap of .NET objects?

In .NET, an object of type value, such as int, is stored in memory.

An object of a reference type requires separate memory allocations for the reference and the object, and the object is stored in a bunch of .NET objects.

And the array is created on the heap, so how is an array of value types, such as int [], stored on the heap? Does this mean that an object of type value can be stored on the heap without a box?

+4
source share
4 answers

Yes you are right. I suggest you read the following:

http://blogs.msdn.com/b/ericlippert/archive/2010/09/30/the-truth-about-value-types.aspx

This is very good, and it explains almost everything that you will ever want to know.

+6
source

Yes, an array is one way that a value of type value can be stored on a heap without a box. Another is just to have it in a regular class:

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

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

This is why the assertion that "value types are stored on the stack, reference types are stored on the heap" is clearly incorrect.

However, since Eric Lippert rightly likes to point out , the distinction between stack and heap is a detail of the implementation. For example, a future version of the CLR may store some objects on the stack if it may be necessary that after completion of this method they will not be needed.

+4
source

Yes, this means that no box is executed for the reach element, because the whole array as a whole is β€œboxed” inside the Array object (although this is not what it called).

In fact, there is no requirement that a value type must be boxed before being heaped. You can put a value type in a bunch in three ways:

  • By wrapping it inside an ordinary object.

  • In boxing.

  • By wrapping it inside an array object.

(There may be more ways, but I don't think I missed them.)

0
source

Think of it this way, the location of an object in memory is determined by the type where it was and where it was declared. If the object is a value type, its value is stored where you specified the variable. If the object is a reference type, its reference is stored where you declared the variable, while the actual instance of the object exists on the heap.

When you declare a local variable, you declare a variable on the stack. Therefore, a value of type value will be on the stack. The reference to the reference type will be on the stack, and the instance of the object is still on the heap.

If you declare an instance variable in a class (reference type), you effectively declare instance variables on the heap. A value of type value will be on the heap (in the instance of the object). The link of the reference type will also be on the heap (in the instance of the object), the instance of the object will be in another place on the heap.

If you declare an instance variable in the structure (value type) where it is located depends on where the base structure was declared.

In the case of an int int[] array, arrays are reference types, and you can think of int values ​​declared as "fields" for this type so that your integers are efficient on the heap.

0
source

Source: https://habr.com/ru/post/1340917/


All Articles