类的字段,它们存储在栈中还是堆中?
我昨天看到一个问题,它(对我来说)提出了另一个问题。请看下面的代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的理解是不正确的。值类型被称为“值类型”,因为它们是按值复制的。引用类型被称为“引用类型”,因为它们是通过引用复制的。 “值类型总是存在于堆栈中”这一说法根本不正确。如果这是真的,它们将被称为“堆栈类型”和“堆类型”。
事实上,这是一个实现细节。不同的框架实现可以根据自己的喜好选择使用栈和堆。 Microsoft 的实现方式如下:
清楚了吗?
字段“A”是值类型。它是一个字段,因此该变量存储在堆上。
实例变量的存储是在堆上,是的。
它永远不会在堆栈上。正如我上面所说,堆栈上唯一的内容是局部变量(以及编译器生成的临时变量),它们不是 lambda 或匿名方法的封闭局部变量,也不在迭代器块中。当然,抖动是自由的,可以使它们完全脱离堆栈,并在有空闲寄存器的情况下将它们放入寄存器中。
但实际上,我不得不问,为什么你关心堆栈上的内容和堆上的内容?堆栈中的内容是我们可以廉价地放入堆栈的内容;其他一切都在堆上。
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:
Is that clear?
The field "A" is of value type. It is a field, and therefore that variable is stored on the heap.
The storage for the instance variables is on the heap, yes.
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.
局部结构(值类型)变量存储在堆栈上,类的值类型字段存储在堆上。
Local struct (value type) variables are stored on the stack, value type fields of a class are stored on the heap.
好吧,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.