Skip to main content

Stack Memory

In Turkish
Stack Belleği
Pronunciation
STAK MEM-uh-ree
Updated 2 min read

Share this page

Send the link, quote the definition with a link back, or show it as a card on your own site.

https://softwaredictionary.org/terms/stack-memory

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

Frames on the call stack and a stack overflow (Python)python
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 exceeded

Readers 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

Spotted a mistake or something missing on this page?Suggest an edit

Read a random page
Open today's review
Switch to the dark theme
Read this page in Türkçe

More

Settings