4 ms·
I'll probably get downvoted, but I think elaborate real-life analogies like this are more likely to confuse novices than help them. A better explanation in my
by codexon 15y ago
I'll probably get downvoted, but I think elaborate real-life analogies like this are more likely to confuse novices than help them.
A better explanation in my opinion is to draw a diagram of a stack and showing that returning from a function just decreases a pointer. C++ programs don't scrub the top of the stack once you finish a function because it is a waste of time.
If another function was called before the 2nd call to foo(), then the variable on the stack would be overwritten.
- pyrtsa 15y agoYeah, the hotel room analogy breaks as soon as you realize that visiting some completely other hotel room will already overwrite that variable in the first one. But it was a fun story, though.
- schleyfox 15y agoWhile that is more accurate and I think that programmers should understand how computers work, as well as C calling conventions, in this case the analogy helps. People who know what the stack is (other than something that can be overflowed) as well as how functions are implemented would likely never think to ask this question. By the time you give the satisfactory "real" answer, you've written the better part of an intro architecture lecture (at my school, I think we probably had 2 or 3 lectures and a lab to get to this point, with other digressions). The real explanation would also be less complete than the analogy as going into the "construction of a football stadium", etc. would have to consider optimizing compilers, virtual memory, and the other things that could get really mucked up. The analogy is accessible even to people who don't program, rather than to just those who have seen %ebp and %esp.
- wh-uws 15y agoLet me preface this by saying I am not attacking you just stating an opinion about the sentiment Thats the exact type of thinking that keeps so many people away from programming and computer science. A nice accessible answer that helps a person make a logical leap in the right direction is so much more important to a novice than pedantry. Its not a perfect answer but to a person who has no clue whats going its a lot more helpful to get some idea
- deleted 15y ago[deleted]
- olavk 15y agoThe analogy is supposed to explain the concept of undefined behavior, not the details of memory management. If you are used to safe languages, the concept of undefined behavior might be confusing. In those languages, an operation is either allowed or not allowed, and if it is allowed it has well-defined behavior. In C a number of operations does not have well-defined results, but is still technically possible to perform, or might be possible depending on circumstances. But you shouldn't use them. The analogy is supposed to explain that.
- sliverstorm 15y agoundefined behavior is fun. All this stuff gets a lot more interesting in microcontrollers, where security is not an issue and you sometimes have to manage memory directly and store data between power cycles.
- ma2rten 15y agoSorry for the off-topic rant, but is it only me or are people, who start with "I'll probably get downvoted, but ...", never actually downvoted? Maybe it's some kind of reverse psychology, like parents who tell their children the opposite of what they actually want them to do. Or maybe the downvoting behavior on HN is actually better then people think and different options are actually valued.
- jamesaguilar 15y agoI've noticed that when people start their comments with "sorry for X," or "is it only me . . ." they tend to get upvoted. This could be another instance of the same reverse psychology you posit.
- gjm11 15y agoSome people (I'm one) make a point of always downvoting people who say "I know I'll get downvoted for this, but ...". (I'll make an exception if what they write is so good I'd have upvoted it; in that case I'll just not upvote it.) The less useful that sort of sympathy-fishing is, the better HN will be.
- iqster 15y agoI'm curious if you considered that the reason people preamble their posts with this is because they have gotten multiple downvotes in the past for something they said which was reasonable? My understanding is that downvotes should not be cast for dissenting opinions or unpopular but logical statements. However, my experience with the site is that this happens from time to time. Thankfully, there is sufficient traffic so that the situation often (but not always) rectifies it self .. i.e. people tend to upvote good comments. I'm relatively new to the site so am still trying to figure it out. To this end, I feel the cap of -10 on downvotes was a great idea. It lets someone post an unpopular (but hopefully logically sound) argument with the knowledge that they will lose a capped amount of karma.
- gjm11 15y ago
- msluyter 15y agoI agree, in particular, that the image of how the stack works is critical for really understanding the problem. The hotel analogy is nice, but doesn't help much in generalizing or extrapolating to other issues.
- scott_s 15y agoAnalogies like this are stepping stones, not the destination. I try to both use an analogy and give the fullest explanation that I think they can handle. The analogy tells them the structure of the complete answer. The complete answer often has concepts that are new to them, and the more unknowns in something, the harder it is to learn. The analogy allows them to understand the structure of the reason, which removes an unknown.