views:

628

answers:

6

Hi all,

In your actual programming experience, how did this knowledge of STACK and HEAP actually rescue you in real life? Any story from the trenches? Or is this concept good for filling up programming books and good for theory?

+3  A: 

Personally, this is one of the very few technical questions that I ask every person I'm going to hire.

I feel that it is critical to understanding how to use the .NET framework (and most other languages). I never hire somebody who doesn't have a clear understanding of memory usage on the stack vs. the heap.

Without understanding this, it's almost impossible to understand the garbage collector, understand .NET performance characteristics, and many other critical development issues.

Reed Copsey
I agree with you, but you really didn't provide a good example of what to know about the Stack and Heap. I am interested in learning something new :)
Stan R.
I agree with leppie, the distinction between a reference and value type is very important, but whether they end up on the stack or heap... you haven't convinced me why it would matter so much.
Meta-Knight
Well, I usually ask in general terms, and try to get the candidate to explain the difference to me. This has become one of my benchmarks on the level of understanding - I feel that somebody who knows how the memory allocation in .NET works will at least be willing and able to learn just about anything else required. I think you need to understand 1)The stack, in general terms, 2)The heap, in general terms, 3)How reference types work, 4)How value types work, 5)Argument passing using ref/out, and how it differs from by value, especially with reference types (not stack/heap, but semi-related)
Reed Copsey
@Meta-Knight: I agree, that's the practical application of this. However, I feel that it's nearly impossible to understand WHY reference/value type semantics work the way they do, and especially how the GC works properly, without understanding stack and heap.
Reed Copsey
One dissenting voice is Eric Lippert of course, who thinks the distinction between reference and value types is much more than stack versus heap (which he describes as an implementation detail). http://blogs.msdn.com/ericlippert/archive/2009/04/27/the-stack-is-an-implementation-detail.aspx
MarkJ
@Reed Copsey: OK, I agree with you then :-) but I'd rather ask an interview question on reference vs value type than stack vs heap!
Meta-Knight
And another dissenting voice in Jon Skeet 'A lot of confusion has been wrought by people explaining the difference between value types and reference types as "value types go on the stack, reference types go on the heap". This is simply untrue' http://www.yoda.arachsys.com/csharp/memory.html
MarkJ
I don't believe this actually answers the question.
sylvanaar
@MarkJ: I never disagreed with you. I was merely pointing out that I think there is a distinction between stack and heap, and I feel that it's an important distinction. I would agree with everyone saying that ref/value type is more critical, but I don't feel that it's possible to fully understand it without understanding the stack and heap. I completely agree with Eric Lippert and Jon Skeet's posts - stack != value type, heap != reference type, and that's one of the things I look for people to understand, too.
Reed Copsey
@sylvanaar: I was suggesting "Concept useful specifically to get job" ;) Prior to my current employment, this has been something that's been asked of me, too. I find that it's a valuable, important thing to understand, because it is required for understanding of many other fundimentals, which my second paragraph was supposed to suggest.
Reed Copsey
@reeed: touche - i guess that is one use of it
sylvanaar
+12  A: 

The distinction in .NET between the semantics of reference types and value types, is a much more important concept to grasp.

Personally, I have never bothered thinking about the stack or heap in all my years of coding (just CLR based).

leppie
Same for me.....
Meta-Knight
mmm - It's difficult to understand reference type vs value type semantics (especially the why behind them) without understanding the stack and heap.
Reed Copsey
Maybe a better question would be: "Explain why value::reference != stack::heap". :)
Craig Stuntz
Jon Skeet has already answered that question http://www.yoda.arachsys.com/csharp/memory.html
MarkJ
+2  A: 

I don't think it matters if you're just building average business applications, which I think most .NET programmers are.

The books I've seen just mention stack and heap in passing as if memorizing this fact is something of monumental importance.

Greg
+6  A: 

It certainly is helpful to understand the distinction when one is building compilers.

Here are a few articles I've written about how various issues in memory management impact the design and implementation of the C# language and the CLR:

http://blogs.msdn.com/ericlippert/archive/tags/Memory+Management/default.aspx

Eric Lippert
A: 

The important distinction is between reference types and value types. It's not true that "value types go on the stack, reference types go on the heap". Jon Skeet has written about this and so has Eric Lippert.

MarkJ