Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>"Heap" and "stack" are outmoded, inaccurate and confusing terms relating to storage duration.</p> <p>Objects with "automatic storage duration" are what silly people call "stack objects". They're the ones that you will define inside a function as "local variables". They go out of scope when their enclosing block ends.</p> <p>Objects with "dynamic storage duration" are those that you create on the <em>free store</em> with the aid of the keyword <code>new</code> (or, if you're silly, <code>malloc</code>), and then destroy whenever you like with the keyword <code>delete</code> (or, if you're silly, <code>free</code>).</p> <p>There are also objects with "static storage duration" that are subject to all sorts of bizarre rules of initialisation order and things. We tend not to use them in idiomatic C++ as much as we can help it.</p> <p>As for the specific variables in your code example, your comments are all accurate, despite the failure in terminology.</p> <p><strong>Addendum:</strong></p> <p>The terms "heap" and "stack" are outdated, relating to back when the most popular runtime libraries used these data structures to store objects which were dynamically- and automatically-allocated, respectively (statically-allocated objects fit into neither category, incidentally).</p> <p>These days that is not always true, and it's certainly not mandated by the C++ standard, which does not care where things are stored. It only cares about how they are created and destroyed, and about how long they live.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload