Heaps Versus Stacks

You might recall from the discussion of local variables and loops that the opening brace of a block is where variables defined inside the block are created and that the block's closing brace causes that storage to disappear. This is true for functions, loops, and if statements.

The storage for these variables is said to be “allocated on the stack.”

A stack is a common “array-like” data structure whose elements are added to one end (pushed) and then taken away from that same end (popped). The compiler generates code to expand and contract a program's single stack of storage each time you call or return from a function and each time you open or close a block.

Figure 13.1 shows what happens when you call the function UserWantsToContinueYorN() ...

Get SAMS Teach Yourself C++ in 10 Minutes SECOND EDITION now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.