What is the difference between the stack and the heap?
This question is likely related to wanting a better understanding of how local variables are stored versus explicitly dynamically allocated variables.
Function parameters and local variables are generally allocated on a stack. A stack, in computer terms, is basically a linear structure in memory where information (values for variables, for example) are stored sequentially with the most recently added value at the “top” or the end of the memory structure from which values are also read and removed. This allows a function to call itself recursively, and have separate copies of all its parameters and local variables retained separately for each call. The variable values are all “pushed” onto the stack, and then execution begins with a new set of values in the new call. When the function returns, the variable values are restored by “popping” the values from the “top” of the stack in reverse order, thus restoring the previous values of all the variables.
A heap is generally used for larger data structures or memory that the programmer wants to explicitly manage. Whenever “new” or “malloc” is used in C++ or C respectively, the memory for that request is allocated on a heap, and then it’s up to the coder to make sure that is gets freed at the appropriate time with “delete” or “free”. Whereas stack data is usually easy to manage because the compiler allocates and frees the space, heap data is easy to mess up because it often requires manually written code to determine when it gets allocated and freed. But it’s appropriate if you want to manage large long-lived blocks of memory independently of the scope of a function. And although all memory in a computer is linear at some level, a heap is less linear in the way it’s managed because it maintains pointers to different areas of memory making it easier to allocate and free blocks of memory in an arbitrary sequence instead of strictly in reverse order.
Stack and heap differ in the following ways:
- Memory Allocation: Stack is used for static memory allocation and Heap for dynamic memory allocation, both stored in the computer’s RAM .
- Variables: Variables allocated on the stack are stored directly to the memory and access to this memory is very fast, and it’s allocation is dealt with when the program is compiled. When a function or a method calls another function which in turns calls another function etc., the execution of all those functions remains suspended until the very last function returns its value. Variables allocated on the heap have their memory allocated at run time and accessing this memory is a bit slower, but the heap size is only limited by the size of virtual memory.
- Access: The stack is always reserved in a LIFO order, the most recently reserved block is always the next block to be freed. This makes it really simple to keep track of the stack, freeing a block from the stack is nothing more than adjusting one pointer. Element of the heap have no dependencies with each other and can always be accessed randomly at any time. You can allocate a block at any time and free it at any time. This makes it much more complex to keep track of which parts of the heap are allocated or free at any given time.
When to use stack and when to use heap?
You can use the stack if you know exactly how much data you need to allocate before compile time and it is not too big. You can use heap if you don’t know exactly how much data you will need at runtime or if you need to allocate a lot of data.
In a multi-threaded situation each thread will have its own completely independent stack but they will share the heap. Stack is thread specific and Heap is application specific. The stack is important to consider in exception handling and thread executions.