类的字段,它们存储在栈中还是堆中?

发布于 2024-08-27 07:12:17 字数 415 浏览 11 评论 0原文

我昨天看到一个问题,它(对我来说)提出了另一个问题。请看下面的代码:

public class Class1
{
   int A; //as I uderstand, int is value type and therefore lives in the stack
}

class Class2
{
    Run()
   {
       Class1 instance1 = new Class1();
       instance1.A = 10;  //it points to value type, but isnt this reference (on heap)?
   }
}

或者在创建Class1的实例时,它的字段类型也在堆上创建?但是我不明白它何时真正位于堆栈上,因为几乎总是需要创建对象的实例才能使用它的字段。

I saw a question yesterday which raised (for me) another question. Please look at the following code:

public class Class1
{
   int A; //as I uderstand, int is value type and therefore lives in the stack
}

class Class2
{
    Run()
   {
       Class1 instance1 = new Class1();
       instance1.A = 10;  //it points to value type, but isnt this reference (on heap)?
   }
}

Or while creating the instance of Class1, its field types are created on the heap as well? But then I do not understand when it would really be on the stack as almost always you need to create an instance of object in order to use it fields.

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

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

发布评论

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

评论(3

对不⑦ 2024-09-03 07:12:17

据我了解,int是值类型,因此存在于堆栈中

您的理解是不正确的。值类型被称为“值类型”,因为它们是按值复制的。引用类型被称为“引用类型”,因为它们是通过引用复制的。 “值类型总是存在于堆栈中”这一说法根本不正确。如果这是真的,它们将被称为“堆栈类型”和“堆类型”。

事实上,这是一个实现细节。不同的框架实现可以根据自己的喜好选择使用栈和堆。 Microsoft 的实现方式如下:

  • 引用类型变量的值是对堆内存的引用。引用基本上是 32 位或 64 位整数。
  • 值类型变量的值就是它的值。
  • 局部变量的值存储在堆栈中,除非局部变量位于迭代器块中或者是匿名方法或 lambda 表达式的封闭外部变量。在这些情况下,局部变量的值存储在堆上。当然,除非局部变量可以被优化掉,在这种情况下根本没有存储空间。或者也许它们可以被注册,在这种情况下它们既不在堆栈上也不在堆上,而是在处理器寄存器中。
  • 引用类型的实例变量和静态变量的值存储在堆上。

清楚了吗?

它指向值类型,但这个引用不是(在堆上)吗?

字段“A”是值类型。它是一个字段,因此该变量存储在堆上。

在创建Class1的实例时,它的字段类型也在堆上创建?

实例变量的存储是在堆上,是的。

但是我不明白它何时真正位于堆栈上,因为几乎总是需要创建对象的实例才能使用它的字段。

它永远不会在堆栈上。正如我上面所说,堆栈上唯一的内容是局部变量(以及编译器生成的临时变量),它们不是 lambda 或匿名方法的封闭局部变量,也不在迭代器块中。当然,抖动是自由的,可以使它们完全脱离堆栈,并在有空闲寄存器的情况下将它们放入寄存器中。

但实际上,我不得不问,为什么你关心堆栈上的内容和堆上的内容?堆栈中的内容是我们可以廉价地放入堆栈的内容;其他一切都在堆上。

as I understand, int is value type and therefore lives in the stack

Your understanding is incorrect. Value types are called "value types" because they are copied by value. Reference types are called "reference types" because they are copied by reference. It is not at all true that "value types always live on the stack". If that were true, they would be called "stack types" and "heap types".

The truth is that this is an implementation detail. Different framework implementations can choose to use the stack and the heap as they like. Here's how the Microsoft implementation does it:

  • the value of a variable of reference type is a reference to heap memory. A reference is basically a 32 bit or 64 bit integer.
  • the value of a variable of value type is its value.
  • the values of local variables are stored on the stack unless the local variables are in an iterator block or are closed-over outer variables of an anonymous method or a lambda expression. In those cases the values of local variables are stored on the heap. Unless of course the local variables can be optimized away, in which case there is no storage at all. Or perhaps they can be enregistered, in which case they are neither on the stack nor the heap, they are in processor registers.
  • the values of instance variables of reference types and static variables are stored on the heap.

Is that clear?

it points to value type, but isn't this reference (on heap)?

The field "A" is of value type. It is a field, and therefore that variable is stored on the heap.

while creating the instance of Class1, its field types are created on the heap as well?

The storage for the instance variables is on the heap, yes.

But then I do not understand when it would really be on the stack as almost always you need to create an instance of object in order to use it fields.

It would never be on the stack. As I said above, the only things that go on the stack are local variables (and compiler-generated temporaries) that are not closed-over locals of a lambda or anonymous method and are not in an iterator block. And of course, the jitter is free to keep them off the stack entirely and put them in registers if there are free registers.

But really, I have to ask, why do you care what goes on the stack and what goes on the heap? What goes on the stack is stuff we can cheaply put on the stack; everything else goes on the heap.

牵你手 2024-09-03 07:12:17

局部结构(值类型)变量存储在堆栈上,类的值类型字段存储在堆上。

Local struct (value type) variables are stored on the stack, value type fields of a class are stored on the heap.

最美的太阳 2024-09-03 07:12:17

好吧,int 是一种值类型,但“1”(对于类来说是一个多么糟糕的名字)是一种引用类型。这意味着“1”的任何实例都在堆上。

Ok int is a value type but '1' (what an awful name for a class) is a reference type. This means that any instance of '1' is on the heap.

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