堆栈是实现细节,还是不是? [英] The stack is an implementation detail, or not?

查看:52
本文介绍了堆栈是实现细节,还是不是?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 http://msdn.microsoft.com/zh-CN/library/ms229017.aspx ,值类型分配在堆栈上或与其他结构内联*".但是在堆栈是一个实现细节,埃里克·利珀特(Eric Lippert)指出,这是一个实现细节.

According to http://msdn.microsoft.com/en-us/library/ms229017.aspx, value types "are allocated on the stack or inline with other structures*". Yet in the stack is an implementation detail, Eric Lippert states that that's an implementation detail.

据我了解,实施细节为";由代码产生的行为,可以通过使用代码来依赖该行为,尽管该行为不是代码编写的规范所指定的." .我知道文档不是规范,尽管大概是如果文档中列出了我们可以依赖的内容,那么它就不再是实施细节.那么,堆栈是实现细节吗?

To my understanding, an implementation detail is "a behavior produced by code which may be relied on by consuming code, though that behavior is not specified by the spec the code is written to.". I understand that documentation is not a specification, though presumably if something is listed in the documentation that we can rely on it and it is no longer an implmentation detail. So, is the stack an implementation detail or isn't it?

*:我理解这意味着结构也可以在其他结构内部分配,而不是直接在堆上分配,尽管我可能是错的.

*: I understand this to mean that structs can also be allocated inside of other ones instead of directly on the heap, though I could be wrong.

推荐答案

MSDN文档向您介绍了Microsoft C#编译器用于 struct 的特定实现.这些特定的细节不在ECMA 334 C#规范中;它们不是 struct 的语义的一部分.因此,文档中的那些细节是实现细节.

The MSDN documentation is telling you about the particular implementation that the Microsoft C# compiler uses for struct. Those particular details are not in the ECMA 334 C# specification; they are not part of the semantics of structs. Therefore, those details in the documentation are implementation details.

我似乎记得在埃里克·利珀特(Eric Lippert)的某处读到他希望(或者更喜欢,我不记得他提供的偏好级别)的文档没有提及与 struct 相关的堆栈s.我会看看是否可以将其挖掘出来.

I seem to remember reading somewhere Eric Lippert saying that he wishes (or prefers, I don't remember what level of preference he provided) the documentation made no mention of the stack in connection with structs. I'll see if I can dig it up.

在这里,是您链接到的博客文章:

Here it is, from the blog post that you linked to:

我很遗憾文档没有集中在最相关的内容上;通过关注与很大程度上不相关的实现细节,我们扩大了该实现细节的重要性,并掩盖了使值类型在语义上有用的因素的重要性.我非常希望所有解释堆栈"的文章将花费时间解释按值复制"的确切含义,以及误解或误用按值复制"会导致错误的原因.

I regret that the documentation does not focus on what is most relevant; by focusing on a largely irrelevant implementation detail, we enlarge the importance of that implementation detail and obscure the importance of what makes a value type semantically useful. I dearly wish that all those articles explaining what "the stack" is would instead spend time explaining what exactly "copied by value" means and how misunderstanding or misusing "copy by value" can cause bugs.

ECMA 334 C#规范的相关部分为§11.请注意,本节中从未使用堆栈"一词.本节仅说明语法,该语法表示 struct 遵循值语义,它们被隐式密封并从 System.ValueType 继承,该赋值给struct 类型创建一个副本,通过值传递 struct 作为参数创建一个副本,如何将 struct s设置为默认值(所有值类型字段)在 struct 中将其设置为默认值,并将所有引用类型字段均设置为 null ),围绕 struct 的装箱和拆箱的规则, struct s的 this 的含义,以及 struct s的字段初始化,构造函数,析构函数和静态构造函数的工作方式.同样,没有提及堆栈.曾经.

The relevant section of the ECMA 334 C# specification is §11. Note that the word "stack" is never used in this section. The section merely spells out the syntax, that structs follow value semantics, that they are implicitly sealed and inherit from System.ValueType, that assignment to a variable of struct type creates a copy, that passing a struct as a parameter by value creates a copy, how structs are set to default values (all value type fields in the struct are set to their default values, and all reference type fields are set to null), the rules surrounding boxing and unboxing of struct, the meaning of this for structs, and how field initialization, constructors, destructors and static constructors work for structs. Again, no mention of stacks. Ever.

堆栈是实现细节,不是 struct 语义的一部分.

The stack is an implementation detail, not part of the semantics of struct.

这篇关于堆栈是实现细节,还是不是?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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