类的字段,都保存在栈或堆? [英] Fields of class, are they stored in the stack or heap?

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

问题描述

我昨天募得(对我来说)另一个问题看到了一个问题。请看看下面code:

 公共类的Class1
{
   诠释A; //我uderstand,int是值类型,因此住在堆
}一流的Class2
{
    跑()
   {
       Class1的实例1 =新1级();
       instance1.A = 10; //它指向的值类型,但是,这不是这个参考(上堆)?
   }
}

或者在创建Class 1的情况下,它的字段类型在堆上创建的呢?但我不明白,当它真的会在堆栈上的几乎总是需要以使用它字段来创建对象的实例。


解决方案

  

据我所知,int是值类型,因此住在堆


您的理解不正确。值类型被称为值类型,因为它们是按值复制。引用类型被称为引用类型,因为它们都通过引用复制。这是不是在所有真实的值类型永远活在堆栈上。如果这是真的,他们将被称为堆型和堆型。

事实是,这是一个实现细节。不同的框架的实现可以选择他们喜欢用栈和堆。下面是Microsoft实现是如何做的:


  • 引用类型的变量的值是堆内存的引用。参考基本上是一个32位或64位整数。

  • 值类型变量的值是它的价值。

  • 局部变量的值存储在堆栈上,除非局部变量是在迭代器块或关闭,在匿名方法或λ前pression的外部变量。在这些情况下,局部变量的值被存储在堆上。当然,除非局部变量可以被优化了,在这种情况下,不存在存储在所有。或者他们可以enregistered,在这种情况下,他们既不是栈也不是堆上,他们是在处理器寄存器。

  • 引用类型和静态变量的实例变量的值存储在堆上。

清楚了吗?


  

它指向的值类型,而不是这个参考(上堆)?


字段A是值类型的。它是一个场,因此该变量保存在堆上


  

在创建Class 1的情况下,是在堆上创建的字段类型呢?


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


  

但是当它真的会在堆栈上的几乎总是需要以使用它字段来创建对象的实例,我不明白。


这绝不会在堆栈中。正如我前面所说,那走在堆栈上的唯一的东西是局部变量(和编译器生成的临时)不在一个lambda或匿名方式封闭了当地居民和不在迭代器块。当然,抖动是免费的,让他们出栈完全,并把它们在寄存器中,如果有免费的寄存器。

不过说真的,我要问,为什么你关心什么进入堆叠上发生的事情堆?善有善报堆栈上的东西,我们可以便宜地放在堆栈上;一切都会在堆上。

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.

解决方案

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.

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

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