Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There is a good question here: </p> <blockquote> <p>"how does it assure there is no conflict between stack allocation and heap allocation?"</p> </blockquote> <p>There is a single contiguous address space in almost all C/C++ implementations, so the stack and heap allocated memory do have to coexist in that space.</p> <p>Although each time the stack grows and shrinks this is not done with individual heap allocations, you can still think of the stack as a single large block of memory allocated from the heap. If the stack grows beyond the boundary of that block, then we have a stack overflow (catchy name... someone should name a website after it).</p> <p>In a multi-threaded program, each time a thread starts, a new stack has to be allocated for it, and when a thread dies the stack can be deallocated. And it would make sense for those whole-stack blocks to be allocated using the same heap management as is exposed through <code>malloc</code>/<code>free</code>.</p> <p>So - very approximately speaking - you can think of the stack as being a type of object that coexists in the heap. A whole stack is <code>malloc</code>-ed all in one go, when a thread starts up, and then it gets suballocated from, and then it gets <code>free</code>-d in one go.</p> <p>On Windows, you can (if you like to live dangerously) <a href="http://www.codeproject.com/KB/cpp/StackShrink.aspx?fid=253775&amp;df=90&amp;mpp=25&amp;noise=3&amp;sort=Position&amp;view=Quick&amp;select=1341672" rel="noreferrer">call the same virtual memory APIs yourself</a> to find out about the stack and to force virtual page within it to be freed.</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