There is a good question here:
"how does he assure that there is no conflict between stack allocation and heap allocation?"
In almost all C / C ++ implementations, there is one contiguous address space, so the allocated stack and heap memory must coexist in this space.
Although every time the stack grows and shrinks, it does not work with separate heap allocations, you can still think of the stack as one large block of memory allocated from the heap. If the stack grows outside this block, then we have a stack overflow (catchy name ... someone should name the site after it).
In a multi-threaded program, every time a thread starts, a new stack must be assigned to it, and when the thread dies, the stack can be freed. And it would be advisable that these blocks of the whole package be distributed using the same heap control as through malloc / free .
So - very roughly speaking - you can think of the stack as the type of object that coexists on the heap. The whole malloc -ed stack is all in one go when the thread starts, and then it gets allocated from it, and then it gets free -d at a time.
On Windows, you can (if you like living life dangerously) call the same virtual memory APIs to find out about the stack and make the virtual page free in it.
Daniel Earwicker Mar 26 '09 at 13:31 2009-03-26 13:31
source share