凡在内存中可空类型存储? [英] Where in memory are nullable types stored?

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

问题描述

这是可能的后续行动提出质疑约可空类型的。

This is maybe a follow up to question about nullable types.

在哪里到底是空值类型(int? ... <$ C C $>)存储在内存中?首先,我认为这显然不够,因为可空&LT; T&GT; 是结构,而这些都是值类型。后来我发现乔恩斯基特的文章在.NET中 记忆,其中说:

Where exactly are nullable value types (int?...) stored in memory? First I thought it's clear enough, as Nullable<T> is struct and those are value types. Then I found Jon Skeet's article "Memory in .NET", which says:

请注意,一个价值型变量   从来没有一个null值 - 它   也没有任何意义,因为空是一个   引用类型的概念,意思是   这种引用类型变量的值   不是参考任何对象在   所有的

Note that a value type variable can never have a value of null - it wouldn't make any sense, as null is a reference type concept, meaning "the value of this reference type variable isn't a reference to any object at all".

我看完这个语句后有点困惑。所以我们可以说我有诠释? A = NULL; 。由于 INT 通常是值类型,它是存储在某种程度上内部结构可空&LT; T&GT; 堆栈(我用正常,因为我不知道值类型会发生什么,当它成为可空)?或其他任何东西在这里发生 - 也许是在堆

I am little bit confused after reading this statement. So let's say I have int? a = null;. As int is normally a value type, is it stored somehow inside struct Nullable<T> in stack (I used "normally" because I don't know what happens with value type when it becomes nullable)? Or anything else happens here - perhaps in heap?

推荐答案

首先,可空&LT; INT&GT; 只是一个速记是这样的:

First off, Nullable<int> is just a shorthand for something like:

struct Nullable<T> 
{
    bool hasValue;
    T value;
}

另外所有构造函数,访问,等等。这就是它 - 一个可空int是一个普通的INT加上一个标志,上面写着整型是否为空或不是。其余全是编译器的魔力,把零作为一个有效的价值;所有的空确实有可空类型是使你的结构与标志设置为false之一。

Plus all the constructors, accessors, and so on. That's all it is -- a nullable int is an ordinary int plus a flag that says whether the int is null or not. All the rest is compiler magic that treats "null" as a valid value; all "null" does with a nullable type is makes you one of those structs with the flag set to false.

所以,现在,我们有出路,你的问题是他们在哪里去记忆?他们去同一个地方,任何其他结构去记忆:在哪里运行时和编译器相信是考虑到内存的寿命最好的地方

So now that we have that out of the way, your question is "where do they go in memory"? They go the same place that any other structs go in memory: where the runtime and compiler believe to be the best place given the lifetime of the memory.

大多数结构去堆。任何人谁告诉你,结构总是在栈上实际上并不知道自己在说什么;我们的文档并没有说,这是不正确的。结构体只去暂时内存池,又名堆栈,当他们是局部变量或临时和局部变量不是封闭的,通过一个匿名方法或lambda外变量和局部变量不是一个iterator块。所有其他结构继续在我们的实现堆。

Most structs go on the heap. Anyone who tells you that "structs always go on the stack" doesn't actually know what they are talking about; our documentation does not say that and it is not true. Structs only go on the temporary memory pool, aka "the stack", when they are local variables or temporaries, and the local variables are not closed-over outer variables of an anonymous method or lambda, and the local variables are not in an iterator block. All other structs go on the heap in our implementation.

请注意也没有要求任何的CLI的实现用堆,使他们的临时池。经典的JScript存储器管理器,例如,存储其上的堆临时池。 (当然,虽然JScript运行时引擎是不是CLI的实现;我只是指出,人们可以设计一个管理的运行时引擎,无论什么穿堆栈没有用户数据),从逻辑上讲它是一个堆栈数据结构,但该数据结构不保存在的栈,它只是在堆上分配的堆叠结构。

Note also that there is no requirement whatsoever that an implementation of the CLI use "the stack" to make their temporary pool. The classic JScript memory manager, for example, stores its temporary pool on the heap. (Though of course the JScript runtime engine is not an implementation of the CLI; I'm merely pointing out that one can design a managed runtime engine that puts no user data whatsoever on "the stack".) Logically it is a stack data structure, but that data structure is not store on "the" stack, it's just a stack structure allocated on the heap.

我要问:你为什么要在乎?在CLR代你管理内存。你为什么关心的地方可空类型去?他们去,他们在有生之年对你有用的地方;你不必担心。

I have to ask: why do you care? The CLR manages memory on your behalf. Why do you care where nullable types go? They go where they live long enough to be useful to you; you don't have to worry about that.

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

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