静态字段在内部究竟是如何工作的? [英] How exactly do static fields work internally?

查看:32
本文介绍了静态字段在内部究竟是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设你有一堂课,

class Foo
{
    public static bar;
}

当你说:

new Foo();

我可以想象在内存中,为这个对象保留了一个空间.

I can imagine that in memory, a space is reserved for this object.

...当你再说一遍:

new Foo(); 

...好吧,现在您有另一个空间可用于该对象.

...well now you have another space available for the object.

然而,静态字段究竟位于何处?

However, where exactly does the static field live?

我真正想学习的是:

对对象的引用如何引用它们引用的对象的相同字段?

推荐答案

虽然类型系统的确切细节取决于实现,但让我更详细地说明这取决于你不应该关心.我将根据 CLR via C# by Jeffrey Richter 和文章 查看 CLR 如何创建运行时对象,作者 Hanu Kommalapati 等人(原始 MSDN 2005 年 5 月发行).

While the exact details of the type system are implementation dependent, let me go into some more detail than just stating that it depends and you should not care. I'll describe how it approximately works in Microsoft's implementation (.NET) according to the book CLR via C# by Jeffrey Richter and the article See How the CLR Creates Runtime Objects by Hanu Kommalapati et al. (original MSDN May 2005 issue).

假设你有一堂课:

class Foo
{
    // Instance fields
    string myBar = "Foobar";
    int myNum;

    // Static fields
    static string bar = "Foobar";
    static int num;
}

Foo myFoo = new Foo();
Type typeOfFoo = typeof(Foo);

实例字段在哪里?

每当你说 new Foo() 时,都会为对象实例分配和初始化空间,并调用构造函数.此实例在下图中显示为 Foo 的实例.例如 instance 只包含类的实例字段(在本例中为 myBarmyNum),并且对于在堆上分配的对象,运行时使用的两个额外字段(同步块索引类型句柄).类型句柄是一个指向 Type 对象的指针,该对象描述了实例的类型,在本例中是 Foo 的类型.

Whenever you say new Foo(), space is allocated and initialized for the object instance, and the constructor is called. This instance is shown as instance of Foo in the image below. Such as instance contains only the instance fields of the class (in this case myBar and myNum), and for objects allocated on the heap two extra fields used by the runtime (Sync block index and Type handle). The type handle is a pointer to a Type object that describes the type of the instance, in this case type of Foo.

当您再次说 new Foo() 时,将分配新空间,该空间将再次包含该类型的实例字段的空间.如您所见,实例字段与对象实例相关联.

When you say new Foo() again, new space is allocated which will again contain space for the instance fields of the type. As you can see, instance fields are associated with object instances.

运行时将每个实例字段放置在距对象数据开头的固定偏移量处.例如,myBar 可能位于偏移量 +4.实例字段的地址就是对象的地址加上字段的偏移量.

The runtime puts each instance field at a fixed offset from the start of the object's data. For example, myBar might live at offset +4. The address of the instance field is simply the address of the object plus the offset of the field.

静态字段在哪里?

C# 和 Java 中的静态字段不与任何对象实例相关联,而是与类型相关联.类、结构和枚举是类型的示例.只有一次(每种类型)分配了一些空间来保存静态字段的值.为描述类型的 Type 结构中的静态字段分配空间是有意义的,因为每种类型也只有一个 Type 对象.这是 C# 和 Java 所采用的方法.

Static fields in C# and Java are not associated with any object instance, but with a type. Classes, structs and enums are examples of types. Only once (per type) is some space allocated to hold the values of the static fields. It would make sense to allocate space for the static fields in the Type structure that describes the type, since there is also only one Type object per type. This is the approach taken by C# and Java.

Type 对象1 是在运行时加载类型时创建的.该结构包含运行时能够分配新实例、调用方法和执行转换等所需的各种信息.它还包含用于静态字段的空间,在本例中为 barnum.

The Type object1 is created when the type is loaded by the runtime. This structure contains all sorts of information needed for the runtime to be able to allocate new instances, call methods and perform casting, among other things. It also contains the space for the static fields, in this case bar and num.

运行时已将每个静态字段放置在距类型数据开头的某个偏移处.这对于每种类型都是不同的.例如,bar 可能位于偏移量 +64.静态字段的地址是Type 对象的地址加上该字段的偏移量.类型是静态已知的.

The runtime has put each static field at some offset from the start of the type's data. This is different for each type. For example, bar might live at offset +64. The address of the static field is the address of the Type object plus the offset of the field. The type is statically known.

1) 在 Microsoft .NET 中,多个不同的结构描述了一个类型,例如 MethodTableEEClass 结构.

1) In Microsoft .NET multiple different structures describe a type, such as the MethodTable and the EEClass structures.

这篇关于静态字段在内部究竟是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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