值类型数组如何存储在 .NET 对象堆中?

发布于 2024-10-18 22:29:37 字数 160 浏览 4 评论 0原文

在.NET中,诸如int之类的值类型对象存储在内存中。

引用类型对象需要为引用和对象单独分配内存,并且对象存储在.NET对象堆中。

而Array是在堆中创建的,那么int[]等值类型的数组如何存储在堆中呢?这是否意味着值类型对象可以存储在堆中而无需装箱?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

柒七 2024-10-25 22:29:37

是的你是对的。我建议您阅读以下内容:

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

这非常非常好,它几乎解释了您想知道的所有内容。

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.

世界如花海般美丽 2024-10-25 22:29:37

是的,数组是一种无需装箱即可将值类型值存储在堆上的方式。另一种是将其放在普通类中:

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

与 Foo 实例关联的所有变量都存储在堆上。 value1 的值只是 int,而 name 的值是字符串引用。

这就是为什么“值类型存储在堆栈上,引用类型存储在堆上”的说法明显不正确。

但是,由于 Eric Lippert 是 正确地指出,堆栈/堆的区别是一个实现细节。例如,未来版本的 CLR 可以在堆栈上存储一些对象,前提是它可以确定在方法终止后不再需要这些对象。

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.

沧桑㈠ 2024-10-25 22:29:37

是的,这意味着没有对reach元素进行装箱,因为整个数组作为一个整体被“装箱”在一个Array对象内(尽管这不是它的名字)。

实际上没有要求说值类型在放入堆之前必须装箱。您可以通过三种方式将值类型放置在堆上:

  1. 将其包装在常规对象内。

  2. 通过装箱。

  3. 将其包装在数组对象内。

可能还有更多方法,但我不认为我错过了任何方法。)

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

冷︶言冷语的世界 2024-10-25 22:29:37

可以这样想,对象在内存中的位置是由它的类型和声明的位置来定义的。如果对象是值类型,则其存储在声明变量的位置。如果对象是引用类型,则其引用存储在声明变量的位置,而实际的对象实例存在于堆上。

当您声明局部变量时,您是在堆栈上声明该变量。因此,值类型的值将位于堆栈上。引用类型的引用将位于堆栈上,而对象实例仍然位于堆上。

如果在类(引用类型)中声明实例变量,则实际上是在堆中声明实例变量。值类型的值将位于堆中(在对象实例中)。引用类型的引用也将位于堆中(在对象实例中),对象实例将位于堆中的其他位置。

如果您在结构体(值类型)中声明实例变量,则它所在的位置取决于声明基础结构体的位置。

对于 int int[] 数组,数组是引用类型,您可以将 int 值声明为该类型的“字段”,因此您的整数是有效地在堆中。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文