Sub topic 11
Stack usage: local arrays, call depth and recursion
This chapter assumes that you can write and upload Arduino sketches, use the Serial Monitor, and understand variables, arrays, functions, and pointers.
What the stack stores
The stack is a temporary area in SRAM. It stores information needed while functions run, including:
- Local variables inside functions.
- Function parameters and return information.
- Saved registers.
- Data used by interrupt handlers.
On common AVR boards such as the Uno, the stack usually starts near the top of SRAM and grows downward. The heap, which supports dynamic allocation, grows upward. Global and static variables occupy a separate fixed area.
A function call creates a stack frame. The frame remains until the function returns. A local array is part of that frame, so even a small-looking declaration can use many bytes:
void readPacket() {
char packet[80];
// Use packet here.
}This function needs at least 80 bytes for packet, in addition to the other data in its frame. The memory becomes available again when readPacket() returns.
Call depth and nested functions
When one function calls another, both functions keep their stack frames until the inner function returns. A chain of calls therefore uses more stack than any one function alone.
For example:
loop()
└─ processReading()
└─ formatReading()
└─ sendReading()If each function has a local buffer, all those buffers can exist at the same time.
Large local arrays are a common source of trouble. Prefer one shared, fixed-size buffer when the calls do not need separate storage. Passing a buffer by pointer also avoids copying the whole array:
void printLabel(const char *text) {
Serial.println(text);
}The pointer is small. The character array remains in its original location.
Recursion and stack exhaustion
Recursion is when a function calls itself. Each recursive call creates another stack frame. The calls must stop at a clearly defined base case.
int sumTo(int number) {
if (number <= 0) {
return 0;
}
return number + sumTo(number - 1);
}This works for small values, but a large value creates many frames. On a small AVR board, recursion can collide with the heap or other stack data. Arduino programs rarely need recursion, so an iterative loop is usually safer:
int sumTo(int number) {
int total = 0;
for (int value = 1; value <= number; value++) {
total += value;
}
return total;
}The loop uses one function frame regardless of the input value.
Reducing stack use
Use these practices when SRAM is limited:
- Keep local arrays small.
- Avoid declaring large buffers inside frequently called functions.
- Reduce unnecessary call depth.
- Replace recursion with loops.
- Pass arrays by pointer instead of copying them.
- Move long-lived buffers to global or
staticstorage when appropriate. - Remember that global or
staticstorage still consumes SRAM. It does not make the data free. - Leave room for interrupts, which also use the stack.
The goal is not to eliminate every local variable. The goal is to ensure that the largest possible combination of active stack frames fits in the SRAM left after global data and heap use.
Exercise
Why can a 100-byte local array cause a failure even when the compiler reports enough unused global SRAM?
Show answer
The array uses the stack at run time. The compiler's global SRAM figure does not include every possible combination of local variables, nested calls, interrupt frames, and heap usage.