类的字段,它们存储在堆栈中还是堆中? [英] Fields of class, are they stored in the stack or heap?

查看:62
本文介绍了类的字段,它们存储在堆栈中还是堆中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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)?
   }
}

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

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.

推荐答案

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

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

事实是,这是一个实现细节.不同的框架实现可以随意选择使用栈和堆.以下是 Microsoft 实现的方式:

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:

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

清楚了吗?

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

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

字段A"是值类型.它是一个字段,因此该变量存储在堆中.

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

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

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.

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

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.

这篇关于类的字段,它们存储在堆栈中还是堆中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆