为值类型的内存分配内部在.NET中引用类型 [英] memory allocation for value type inside reference type in .net

查看:161
本文介绍了为值类型的内存分配内部在.NET中引用类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得这是一个很琐碎的问题,但我不能得到一个明确的答案,它在网络上。

我有一个包含值类型和引用类型的类。考虑到在类的引用类型将得到执行,内存是如何分配的这样一类的每个成员在某个实例?指针是如何被存储和访问每个这些部件的?还创建了类型,其中存储结构,如堆栈或堆?

我知道这多,如果值类型只有一个变量用于我的code那么它的值和所有其他的详细信息,如它的类型等被存储在堆栈中。同样,如果引用类型实例化则实际对象在堆中创建的指针和指向这个存储器位置存储在堆栈中。但对于值类型$ P ​​$ psent在类(引用类型)?他们在哪里保存,它们是如何进入?

我已经给下面这样的类的实例。在引用这一类的回答将是非常有益的。

 公共类员工
{
    公众诠释EMPNO {获得;组; }
    公共字符串EmpName {获得;组; }
    公共BankAccDetails AccDetails {获得;组; }
}

公共类BankAccDetails
{
    //这里其它性能
}
 

解决方案
  

但是,我们在类(引用类型)值类型$ P ​​$ psent?   他们在哪里保存,它们是如何进入?

值类型存储在那里声明。根据你的情况,他们将在堆。

但是,你应该看到下面的文章就内存管理在C#。

真相约值类型 - 埃里克利珀

  

在C#中的桌面上的CLR,值类型的Microsoft实现   被存储在栈上时的值是一个局部变量或   暂时不能是拉姆达的封闭区切换局部变量或   匿名的方法,并且该方法的身体不是迭代器块,及   抖动选择不enregister值

<一个href="http://blogs.msdn.com/b/ericlippert/archive/2009/04/27/the-stack-is-an-implementation-detail.aspx"相对=nofollow>堆栈是一个实现细节,第一部分 - 埃里克利珀
在.NET 记忆 - 什么去哪里 - 乔恩斯基特

I think this is a very trivial question but I am not able to get a definite answer to it on the net.

I have a class which contains both value types and reference types. Considering the reference types in the class will get instantiated sometime during the execution, how the memory is allocated for each member of such a class? How the pointer is stored and accessed for each of these members? Also which type is created in which memory structure, i.e. stack or heap?

I know this much that if only a variable of value type is used in my code then its value and all the other details like its type, etc are stored in the stack. Similarly if a reference type is instantiated then the actual object is created in the heap and a pointer to this memory location is stored in the stack. But what about value types present inside a class (reference type)? Where are they stored and how are they accessed?

I have given an example of such a class below. An answer in reference to this class will be very helpful.

public class Employee
{
    public int EmpNo { get; set; }
    public string EmpName { get; set; }
    public BankAccDetails AccDetails { get; set; }
}

public class BankAccDetails
{
    //Other properties here
}

解决方案

But what about value types present inside a class (reference type)? Where are they stored and how are they accessed?

Value types are stored where they are declared. In your case, they will be on heap.

But you should see the following articles with respect to memory management in C#.

The Truth about value types - Eric Lippert

in the Microsoft implementation of C# on the desktop CLR, value types are stored on the stack when the value is a local variable or temporary that is not a closed-over local variable of a lambda or anonymous method, and the method body is not an iterator block, and the jitter chooses to not enregister the value.

The Stack Is An Implementation Detail, Part One - Eric Lippert
Memory in .NET - what goes where - Jon Skeet

这篇关于为值类型的内存分配内部在.NET中引用类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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