对象如何存储在内存中? [英] How are objects stored in memory?

查看:75
本文介绍了对象如何存储在内存中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解如何使用.NET Framework将对象存储在内存中.

I'm trying to understand how objects are stored in memory using the .NET Framework.

给出以下 person 类:

public class Person
{
    public string name { get; set; }

    public int age { get; set; }
}

我相信 Person 类型的启动变量在内存中将具有以下结构:

I believe an initiated variable of type Person would have the following structure in memory:

问题:

  • 首先,根据我的理解,它们是否存在主要/明显的缺陷?(我几乎可以肯定,因为按照我所描述的方式处理对象似乎效率低下;尤其是 name 指针指向字符串的char集合的方式)成员)

  • Firstly, are their any major / obvious flaws in my understanding? (I'm almost certain there is, as it just seems way to in-efficient to handle objects in the way I am describing; especially in the way the name pointer points to the char collection for the string member)

其次,对于类的值类型成员(即 Age ),它们是存储在对象本身内(因此与对象位于相同的内存地址内),还是得到它们?分配自己的地址,然后指向该对象呢?(如我的图所示)

Secondly, for value type members of a class (I.E Age), are they stored within the object itself (so within the same memory address as the object), or do they get allocated their own address, and the object then points to it? (Such as my diagram shows)

类似于以上问题,但对于引用类型成员,对象是否持有指向该指针的指针?(即,名称指针引用了我的图表中的char集合)

Similar to the above question, but for reference type members, does the object hold a pointer to the pointer? (I.E The name pointer referencing the char collection in my diagram)

最后,如果我的 Person 类的成员是字段而不是属性,会有所作为吗?

Finally, Would it make a difference if the members of my Person class were fields rather than properties?

更新:根据Sweeper&的答案更新了图表.蒂姆,我认为它是正确的.

Update: Updated diagram based on answers from Sweeper & Tim, which I believe is now correct.

注意:指针已更改为引用,因为这是托管代码.

Note: Pointer changed to reference as this is managed code.

推荐答案

我将首先回答最简单的问题:

I'll start with the easiest-to-answer question:

如果我的Person类的成员是字段而不是属性,那会有所不同吗?

Would it make a difference if the members of my Person class were fields rather than properties?

不.您代码中的属性只是语法糖.编译代码时,这些属性使用 {get;放;} 将被转换为带有getter和setter的字段.

No. The properties in your code are just syntactic sugar. When the code is compiled, these properties with { get; set; } will be turned into fields, with a getter and a setter.

我的理解中是否存在重大/明显的缺陷?

are there any major / obvious flaws in my understanding?

是的.当我回答以下问题时,我会提到他们.

Yes. I will mention them when I answer the below questions.

第二,对于类的值类型成员(IE年龄),它们是存储在对象本身内(因此与对象位于相同的内存地址中),还是为它们分配了自己的地址,然后该对象指向要吗?(如我的图所示)

Secondly, for value type members of a class (I.E Age), are they stored within the object itself (so within the same memory address as the object), or do they get allocated their own address, and the object then points to it? (Such as my diagram shows)

您的第一个陈述是正确的.值类型存储在对象中. Person 对象中没有指向 int 的指针.换句话说,您的图表是错误的.

Your first statement is correct. Value types are stored within the object. There isn't a pointer in a Person object pointing to an int. In other words, your diagram is wrong.

但对于引用类型成员,对象是否持有指向该指针的指针?(即,名称指针引用了我的图表中的char集合)

but for reference type members, does the object hold a pointer to the pointer? (I.E The name pointer referencing the char collection in my diagram)

在这种情况下,您已经注意到 char [] 是引用类型.但实际上它持有一堆 char ,它们是值类型.所以不,字符存储在char数组中,就像 Age 存储在 Person 中一样.另一方面,如果这是一个 string [] ,则将存在一个指向数组的指针,该指针将包含指向字符串对象的指针.这也意味着您的图表是错误的.

In this case, char[] is a reference type as you have noticed. But it's actually holding a bunch of chars, which are value types. So no, the characters are stored within the char array, just like the way Age is stored in Person. On the other hand, if this were a string[], there would be a pointer pointing to the array which would contain pointers pointing to string objects. This also means that your diagram is wrong.

这篇关于对象如何存储在内存中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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