Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> Well, actually, in C# struct instances ARE objects allocated on the stack, instead of the heap.

You have it backwards, they're not value types because they're allocated on the stack, structs are allocated on the stack because they're value types.

See Eric Lippert's article "The Stack is an Implementation Detail": http://blogs.msdn.com/b/ericlippert/archive/2009/04/27/the-s...



So no, I don't have it backwards.

Also Eric Lippert nails it when he answers the question of why reference types are not stack allocated and value types are ... “because they can”.

I do agree with the article, but with all due respect to Eric Lippert, C#/.NET was meant to be reasonably fast for all kinds of user-land applications and if structs weren't added, then they had to add special cases (primitives) for dealing with integer and floating point arithmetic, just as Java did.

I do agree that structs have semantic value, but the implementation itself allows the available primitives to be described in terms of structs, which is a really elegant and cost-effective solution to a problem that the JVM engineers are trying to solve with complicated tricks like escape analysis. If you remove the performance/efficiency benefit, there isn't a lot of value left in structs - at least nothing that can't be solved by immutable data-structures and/or a better type system.

Nice article btw, thanks.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: