<story><title>Q: Why can I access an out-of-scope C++ var? A: So you rent a hotel room...</title><url>http://stackoverflow.com/questions/6441218/c-local-variable-can-be-accessed-outside-its-scope/6445794#6445794</url></story><parent_chain><item><author>codexon</author><text>I&apos;ll probably get downvoted, but I think elaborate real-life analogies like this are more likely to confuse novices than help them.&lt;p&gt;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&apos;t scrub the top of the stack once you finish a function because it is a waste of time.&lt;p&gt;If another function was called before the 2nd call to foo(), then the variable on the stack would be overwritten.</text></item></parent_chain><comment><author>olavk</author><text>The analogy is supposed to explain the concept of &lt;i&gt;undefined behavior&lt;/i&gt;, not the details of memory management.&lt;p&gt;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&apos;t use them. The analogy is supposed to explain that.</text></comment>
<story><title>Q: Why can I access an out-of-scope C++ var? A: So you rent a hotel room...</title><url>http://stackoverflow.com/questions/6441218/c-local-variable-can-be-accessed-outside-its-scope/6445794#6445794</url></story><parent_chain><item><author>codexon</author><text>I&apos;ll probably get downvoted, but I think elaborate real-life analogies like this are more likely to confuse novices than help them.&lt;p&gt;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&apos;t scrub the top of the stack once you finish a function because it is a waste of time.&lt;p&gt;If another function was called before the 2nd call to foo(), then the variable on the stack would be overwritten.</text></item></parent_chain><comment><author>schleyfox</author><text>While 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 &quot;real&quot; answer, you&apos;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).&lt;p&gt;The real explanation would also be less complete than the analogy as going into the &quot;construction of a football stadium&quot;, 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&apos;t program, rather than to just those who have seen %ebp and %esp.</text></comment>