值类型和引用类型存储 [英] Value Type and Reference Type Storage

查看:80
本文介绍了值类型和引用类型存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

值类型存储在堆栈中

引用类型在堆上分配内存。



引用类型具有值类型,例如,类可以有基本的int属性或结构,结构可以有一个类对象。

那么类/结构如何存储在内存中。

结构是否在堆栈或堆中获取内存分配,结构中包含的类在哪里。

类似地,具有值类型的类存储在堆中但是类的成员在哪里存储像int变量等。

A value type is stored in a stack
A reference type is allocated memory on a heap.

A reference type has value types, eg class can have basic int properties or structs and structs can have a class object.
Then how does the class/struct get stored in the memory.
Does the struct gets memory allocation in stack or heap and where is the class contained in the struct goes.
Similarly a class having the value type is stored in heap however the where are the members of the class are stored like int variables etc.

推荐答案

这是错误的。并非所有值类型都保存在堆栈中。



许多开发人员认为引用类型存储在堆上,而值类型总是存储在堆栈中 - 这不是完全正确。



首先它更多的是实际运行时的实现细节而不是语言要求但更重要的是它不可能 - 考虑一个类(引用类型)有一个整数成员(值类型),该类存储在堆上,因此它的成员包括值类型,因为它的数据是按值复制的。



Thats wrong. Not all value types are saved on the stack.

Many developers believe that reference types are stored on the heap while value types are always stored on the stack – this is not entirely true.

First it’s more of an implementation detail of the actual runtime and not a language requirement but more importantly it’s not possible – consider a class (reference type) which has a integer member (value type), the class is stored on the heap and so are it’s members including the value type since its data is copied "by-value".

class MyClass
{
    // stored in heap
    int a = 5;
}


你可以阅读Eric Lippert的帖子 - http://blogs.msdn.com/b/ericlippert/archive/2009/04/27/the-stack -is-an-implementation-detail.aspx [ ^ ]



别忘了将其标记为答案如果它对你有所帮助祝你好运!!
You can read Eric Lippert''s post - http://blogs.msdn.com/b/ericlippert/archive/2009/04/27/the-stack-is-an-implementation-detail.aspx[^]

Don''t forget to mark it as answer if it helped you. Good luck!!


没有。这不是那么简单。值类型对象不会仅存储在堆栈中。这取决于它们的使用位置。它们存储在1)堆栈中; 2)为静态成员保留的内存,3)在堆上,当用作引用类型(类)的成员时。



当一个类被实例化时,它也是没那么简单。所有引用变量/成员都占用内存中的某些位置。假设您有一个引用类型的局部变量。它在堆栈上分配。然后你实例化它,或者为它分配已经实例化的变量。引用本身没有任何反应 - 它仍然在堆栈中。但是引用的值开始引用堆栈上的一些内存。在第二种情况下,当您分配已经实例化的引用时,您有两个引用引用堆上的同一对象。



将引用视为托管指针(C ++ / CLI)术语)。它们不是真正的指针(这不是讨论托管系统如何工作的地方),但它们就像存储方面的指针一样:指针指向某个对象(在.NET的情况下,这总是托管堆;我不讨论这里使用指针语法的 unsafe 指针,但指针本身会占用一些内存。哪里?再次:在堆上,在堆栈上,在静态内存区域。



在所有情况下,你应该考虑所有链:什么包含什么。



-SA
No. This is not that simple. Values type objects are not stored just on stack. It depends on where they are used. They are stored on 1) stack; 2) memory reserved for static members, 3) on heap, when are used as a members of reference types (classes).

When a class is instantiated, it also not so simple. All reference variables/members take some place in memory. Suppose you have a local variable of a reference type. It is allocated on stack. Then you instantiate it, or assign already instantiated variable to it. Nothing happens to the reference itself — it remains on stack. But the value of the reference starts referencing some memory on stack. In second case, when you assign an already instantiated reference, you got two references referencing the same object on heap.

Consider references as "managed pointers" (C++/CLI terminology). They are not "real" pointers (this is not a place to discuss how managed system works in detail), but they are like pointers in terms of storage: the pointer "points" to some object (in case of .NET this is always the managed heap; I don''t discuss unsafe pointers here which use the pointer syntax), but the pointer itself takes up some memory. Where? Again: on heap, on stack, in the "static" memory area.

In all cases, you should consider all the chain: what contains what.

—SA


这篇关于值类型和引用类型存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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