views:

117

answers:

2

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.

A: 

For C# value types the 'stack' is an implementation detail because every safe operation performed on the value type will behave the same way, orthogonal to whether the struct was allocated on stack or on heap.

Operations for which it would make a difference (ie. end up in referencing a deallocated stack frame), like taking the address and using it directly (eg. via legacy APIs), are unsafe and erroneous usage (ie. not using a Marshaling API).

Remus Rusanu
+8  A: 

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.

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.

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.

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

Jason