Stack (Memory)
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.



