EmbeddedRelated.com

Stack (Memory)

Category: Memory | Also known as: stack

A stack is a region of RAM used for automatic storage of local variables, function return addresses, and processor register states, operating on a Last-In, First-Out (LIFO) basis. It is managed by the CPU via a stack pointer register that increments or decrements as data is pushed or popped.

In practice

In embedded systems, the stack is typically allocated at build time by the linker script. Unlike desktop systems with virtual memory, embedded MCUs have a fixed stack size. If a program exceeds this limit, it results in a stack overflow, which often leads to silent data corruption or hard faults. Developers must carefully estimate the maximum call depth and local variable usage to set an appropriate stack size.

Techniques such as 'stack painting' or 'stack canaries' are common for monitoring usage. By filling the stack area with a known magic pattern (e.g., 0xDEADBEEF) at startup, developers can later examine memory to see how much of the pattern was overwritten. This process is described in the blog post 'Examining The Stack For Fun And Profit' as a way to determine high-water marks during testing.

Embedded developers often avoid large local arrays or deep recursion to prevent overflows. In Real-Time Operating Systems (RTOS), every task is assigned its own independent stack. Miscalculating the stack requirements for a single task is a frequent cause of system instability. The blog post 'Are We Shooting Ourselves in the Foot with Stack Overflow?' highlights the risks of neglecting these limits in resource-constrained environments.

Discussed on EmbeddedRelated

Frequently asked

How does the stack differ from the heap?
The stack is managed automatically by the compiler for local variables and function calls, while the heap is used for dynamic memory allocation managed manually by the programmer via malloc and free.
What happens during a stack overflow?
The stack pointer moves beyond its allocated boundary and begins overwriting adjacent memory, such as global variables or the heap, often leading to unpredictable crashes or hard faults.
Why should recursion be avoided in embedded systems?
Recursion can consume stack space rapidly and unpredictably; since embedded memory is limited, recursive algorithms like those found in 'Flood Fill, or: The Joy of Resource Constraints' can easily cause a stack overflow.
What is a stack pointer (SP)?
The stack pointer is a hardware register in the CPU that holds the memory address of the current top of the stack.
How do interrupts affect the stack?
When an interrupt occurs, the CPU automatically pushes the current program counter and status registers onto the stack to ensure the processor can return to the exact state it was in before the interrupt.

Differentiators vs similar concepts

The stack handles automatic, short-lived data tied to function scope, whereas the heap handles long-lived data with manual lifecycle management. Static memory differs from both by being allocated at compile time for the entire duration of the program execution.