TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

What and where are the stack and heap?

85 pointsby gs7over 11 years ago

5 comments

derefrover 11 years ago
If this article is news for you, this might be a helpful corollary:<p><i>Threads</i> and <i>processes</i> are both units of execution. (A unit of execution is like a virtual processor -- a separate instruction pointer that makes its way through your code, doing computations and following jumps.)<p>Both <i>threads</i> and <i>processes</i> have their own stacks. You reserve a new stack when you fork(), or when you spawn a thread.<p>But only <i>processes</i> have their own heaps. In fact, this is the defining distinction between threads and processes: if you&#x27;re sharing your heap with other execution-units, you&#x27;re a thread; if you have your own isolated heap, you&#x27;re a separate process.
评论 #6867754 未加载
评论 #6867896 未加载
terhechteover 11 years ago
Uli Kusterer has a really good, very graphical, description in his &quot;Masters of the Void&quot; series, too:<p><a href="http://masters-of-the-void.com/book5.htm" rel="nofollow">http:&#x2F;&#x2F;masters-of-the-void.com&#x2F;book5.htm</a>
com2kidover 11 years ago
I&#x27;d argue against there being &quot;only 1 heap&quot;. Only one OS provided heap, sure, but in C++ it is quite common to overload new and allocate into a separate statically declared buffer.<p>(See: Video games!&quot;<p>And, as usual, no mention was made of static allocations! :(
评论 #6867708 未加载
评论 #6867716 未加载
评论 #6868325 未加载
mtdewcmuover 11 years ago
The stack is a concept that&#x27;s built into the CPU to handle function calls. Every time you call a function, there is a return address that needs to be stored. Since you need to hang on to a return address for every function that is currently in progress, and you use them in the reverse order that they were stored, what you need is exactly a stack. The stack is usually (always?) located at the top of memory and grows down. In addition to return addresses, it can be used to store data associated with an in-progress function, like arguments, local variables, and cpu state data that needs to be temporarily stored.<p>So that&#x27;s the stack. The concept of the heap, on the other hand, isn&#x27;t built into the CPU, and is sort of a vague abstraction implemented by programming languages. The first thing to know about how it differs from the stack: it&#x27;s not a stack. Stack reads and writes follow LIFO; reads and writes to the heap can potentially be in any order. The heap is usually (always?) located close to the bottom of memory, and grows up. So the stack and heap grow toward each other. At the lowest level, the heap is basically just memory that can be used as the application sees fit. Data on the heap isn&#x27;t inherently tied to an in-progress function call, so it can last indefinitely (at least, until the process ends). The programming language or the C library typically provide a means to subdivide the heap memory into discrete blocks as needed in response to an explicit or implicit memory allocation request (e.g. malloc(), new), and keep track of what memory is available and what is currently allocated.<p>Differences between the stack and the heap:<p>* the stack is limited in size, so it is easy to overflow and crash if you try to store too much data there. the heap, in contrast, is designed to grow as much as needed * the stack is managed primarily by instructions built into the cpu. the heap is managed by library calls and system calls * the difference in speed between the two refers to the speed of allocating or freeing a chunk. the stack can inherently do this quickly, because the location for the memory is predetermined. since the heap can do allocations and deallocations in any order, finding memory for blocks is inherently more complicated and slower. * since the fundamental purpose of the stack is to store data about in-progress functions, it follows that every executing thread has its own stack and usually only accesses its own. the heap, on the other hand, has no association to a thread of execution, which consequently means that multiple threads can share the same heap, and locking and synchronization must come into play when the heap metadata has to change. The locking is typically transparent, but the net result is to make allocations and deallocations even slower in threaded code.
评论 #6868187 未加载
评论 #6868333 未加载
pramodliv1over 11 years ago
I remember the first time when I worked on multithreaded C++ project. I allocated a structure on the stack and passed its address to a queue which was received by another thread and the inevitable segfault happened. My mistake provided a lot of laughs for the entire team.<p>Also see <a href="http://stackoverflow.com/questions/8468210/stack-vs-heap-c" rel="nofollow">http:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;8468210&#x2F;stack-vs-heap-c</a>
评论 #6869184 未加载