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

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

问题描述

假设你有一个类,

class Foo
{
    public static bar;
}

当你说:

new Foo();



我可以想像,在内存中,一个空间保留给这个对象。

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

... ...当你说一次:

...and when you say again:

new Foo(); 



...现在好了你们有对象的另一个空间。

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

然而,正是它的静态字段住

However, where exactly does the static field live?

什么我真的想了解的是:

What I am really trying to learn is:

如何到对象的引用引用对象的同一领域,他们引用?

推荐答案

虽然类型系统的具体细节依赖于实现,让我去到一些细节不仅仅是指出的这取决于的和的你不应该在乎的。我会按照书 CLR通过C#<描述如何在大约微软的实现(.NET)的作品/ EM >按杰弗里里希特和文章的查看CLR如何创建运行时对象的由Hanu Kommalapati等(的原M​​SDN 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);



在哪里实例字段住?

无论何时说新富(),空间分配和对象实例的初始化,构造函数被调用。此实例如下图所示的图像中富的的的实例。如实例包含唯一的类的实例字段(在这种情况下, myBar 的myNum ),以及分配的对象上使用运行时堆了两个附加字段(同步块索引键入句柄)。该型手柄是一个指向描述实例的类型,在这种情况下,键入对象的 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.

当你说新富()再次,新的空间分配,这将再次包含空格该类型的实例字段。正如你所看到的,实例字段与对象相关的实例

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静态字段不与任何对象实例相关联,但与一类型。类,结构和枚举就是类型的示例。只有一次(每个类型)是分配给保持静态字段值的一些空间。它将使意义,为中描述的类型键入结构的静态字段分配空间,因为还只有一个键入每个类型的对象。这是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.

键入对象 1 是当类型是由运行时加载创建。此结构包含所需的运行时的各种信息,以便能够分配新的情况下,调用方法和执行铸造,除其他事项。它还包含静态字段的空间,在这种情况下 NUM

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.

运行时已​​经把每一个静电场在某从类型数据的起始位置的偏移。这是对每个类型不同。例如,可能住在偏移+64。静态字段的地址是键入对象的地址加场的偏移量。该类型是静态已知的。

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多个不同的结构描述的类型,如在<青霉>方法表 EEClass 的结构。

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

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

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