Quiz Entry - updated: 2026.07.10
What determines whether a function needs a stack frame at all?
A function needs stack space only when it can't keep everything in registers — e.g. too many locals, an &-taken local, arrays/structs, callee-saved regs to preserve, or making calls itself.
Many small functions need no frame at all. The compiler allocates one only when forced to, by any of these:
- Too many locals to fit in registers
- Address-of (
&x) on a local — it must have a real memory address - Arrays or structs as locals (they live in memory)
- Calling other functions — may need to spill caller-saved registers
- More than 6 arguments to pass to a callee (extra args go on the stack)
- Using callee-saved registers that must be saved/restored
A pure leaf function often compiles with zero stack operations:
long add(long x, long y) { return x + y; }
add:
leaq (%rdi,%rsi), %rax # no push/pop, no sub $N,%rsp
ret
Whereas a function that calls others usually needs a frame to hold values across those calls.
Reverse-engineering tip: the presence of a prologue/epilogue and the size of the sub $N,%rsp already tell you a lot about how much local state a function juggles.
Go deeper:
Function prologue and epilogue (Wikipedia) — the frame setup a function may or may not need.