Stack Memory
- In Turkish
- Stack Belleği
- Pronunciation
- STAK MEM-uh-ree
In short
Stack memory is where a thread keeps its functions' local variables and return addresses, growing with each call and shrinking automatically on return.
What is stack memory?
Every function call pushes a stack frame onto the thread's stack: the function's parameters, local variables and the address to return to. When the function returns, its frame is popped and the memory is instantly reusable. Because frames are added and removed in last-in, first-out order, allocation is just moving a pointer, which makes the stack extremely fast.
Each thread has its own stack, and its size is fixed and fairly small: a few megabytes is typical, often 8 MB for the main thread on Linux and 1 MB on Windows. Values on the stack must have a size known in advance and live only as long as the function that created them. Anything larger, variable-sized or longer-lived goes on the heap.
When a program crashes, the stack trace it prints is a snapshot of the call stack: which function called which, down to the line that failed. Debuggers show the same frames, letting you inspect each function's local variables at the moment it stopped.
A common misconception is that a stack overflow only happens in badly written code. It happens whenever the stack runs out of space, most often through recursion that goes too deep or never ends, but also through very large local arrays. Converting deep recursion into a loop, or moving big buffers to the heap, fixes it.
Key takeaways
- The stack holds each function call's frame: parameters, locals, return address.
- Frames are pushed on call and popped on return, so allocation is very fast.
- Each thread has its own small, fixed-size stack.
- A stack trace is a snapshot of the call stack when an error happens.
- Deep or endless recursion causes a stack overflow.
Example
def total(prices):
subtotal = sum(prices) # 'prices' and 'subtotal' live in this call's frame
return add_tax(subtotal)
def add_tax(amount):
return amount * 1.2 # a new frame on top; popped when it returns
print(total([10, 20])) # frames: <module> → total → add_tax
def countdown(n):
return countdown(n - 1) # no base case: every call adds a frame
countdown(10) # RecursionError: maximum recursion depth exceededReaders ask
What is the difference between stack and heap memory?
The stack stores function call frames and is managed automatically in last-in, first-out order, which is fast but small and short-lived. The heap stores data that is allocated at any time, can be large and lives until it is freed or garbage collected, which is more flexible but slower.
What causes a stack overflow?
Using more stack space than the thread has, usually through very deep or infinite recursion, or by declaring huge local arrays. The program then crashes or raises an error such as RecursionError or StackOverflowError.
What is a stack frame?
The block of stack memory for one function call, holding its arguments, local variables and the return address. A new frame is created on each call and removed when the call returns.
Often compared
See also
- Heap MemoryOperating Systems, p. 14Heap memory is the region for data a program allocates at runtime, whose size or lifetime isn't known in advance and can outlive the function that made it.
- StackData Structures, p. 31A stack is a data structure that stores items in last in, first out (LIFO) order, so the most recently added item is always the first one removed.
- RecursionProgramming Fundamentals, p. 48Recursion is a technique in which a function solves a problem by calling itself on smaller versions of the same problem until it reaches a simple base case.
- ThreadOperating Systems, p. 33A thread is the smallest unit of execution an operating system can schedule, running inside a process and sharing that process's memory with other threads.
- FunctionProgramming Fundamentals, p. 21A function is a named, reusable block of code that performs a specific task, optionally taking inputs called parameters and returning a result.
- Memory LeakOperating Systems, p. 19A memory leak is a bug where a program keeps holding memory it no longer needs, so its memory usage grows over time and can slow down or crash the system.
Spotted a mistake or something missing on this page?Suggest an edit