Stack Size Calculator

Estimate the required stack size for an embedded application based on call depth, local variables, and interrupt overhead.

Deepest function call nesting

bytes

Return address + saved registers + locals

bytes
%

Recommended Stack Size

480 bytes

Base Call Stack256 bytes
Interrupt Stack128 bytes
Total Before Margin384 bytes

Recommended Stack Size vs Max Call Depth

Formule

## Estimating Stack Size for Embedded Systems Stack overflow is a common and dangerous bug in embedded systems. Proper sizing prevents crashes. ### Formula **Base Stack = Max Call Depth x Avg Frame Size** **ISR Stack = Interrupt Levels x ISR Frame Size** **Recommended = (Base + ISR) x (1 + Safety Margin)** The safety margin accounts for worst-case alignment, compiler-generated temporaries, and unforeseen call paths. A 25% margin is typical for production systems.

Exemple Résolu

8-deep call stack, 32-byte frames, 2 interrupt levels at 64 bytes each, 25% safety.

  1. 01Base stack: 8 x 32 = 256 bytes
  2. 02ISR stack: 2 x 64 = 128 bytes
  3. 03Total raw: 256 + 128 = 384 bytes
  4. 04With 25% margin: 384 x 1.25 = 480 bytes

Questions Fréquentes

How do I measure actual stack usage?

Fill the stack with a known pattern (e.g., 0xDEADBEEF) and check after running to see how far the pattern was overwritten.

What happens on stack overflow?

The stack corrupts adjacent memory, causing unpredictable behavior, hard faults, or silent data corruption.

Should I use a separate stack for ISRs?

Some architectures (ARM Cortex-M) have a separate MSP for interrupts. If available, use it to isolate ISR stack from task stacks.

Apprendre

Ohm's Law Guide

Calculatrices Associées