The Runtime Theory
easyApplicationDSA#stack#calling-conventions#memory

What happens on the stack when you call a function?

Probes whether you understand stack frames, the return address, and caller/callee conventions — not just the slogan 'the stack grows down'. A strong answer walks the call through push, jump, and unwind.

The Runtime Theory Team2 min readasked at google · amazon · microsoft · datadog

This question tests whether you can describe the machine, not the abstractions. The interviewer wants to hear about the call stack as a region of memory with a hardware stack pointer (rsp), not "the stack is where local variables go."

The mental model: the call stack is a contiguous chunk of memory per thread, growing downward on x86-64 and ARM64. Calling a function means pushing enough state to resume the caller when the callee returns. That state is the stack frame.

Walk through the mechanism. On a typical ABI, the caller first saves any arguments that don't fit in registers, or leaves them in registers per the calling convention. Then it executes call, which pushes the return address — the address of the instruction right after the call — onto the stack and jumps to the function. The callee's prologue subtracts from rsp to reserve space for its own locals, saves the previous frame pointer (rbp) if using frame pointers, and optionally saves callee-saved registers it intends to clobber. The frame now contains, from low to high address: locals, saved registers, the saved frame pointer, the return address, then the caller's frame.

Locals are addressed relative to rsp or rbp — that's the "variable slot" you think of as a local variable. When the callee returns, the epilogue restores the saved registers and frame pointer, executes ret, which pops the return address and jumps to it, and the caller continues exactly where it left off. The stack pointer is restored to its original value, so the whole frame is reclaimed in one instruction — no individual deallocation.

Tradeoffs and edge cases worth naming:

  • Frame pointers vs -fomit-frame-pointer. Without rbp chaining you lose cheap stack walking, which is why debuggers and profilers want them.
  • Stack overflow is not "too deep recursion" conceptually — it's the stack pointer colliding with the stack guard page, an unmapped page that triggers a segmentation fault when touched.
  • Calling conventions (System V, Windows x64, ARM64 AAPCS) differ in argument registers, shadow space, and who cleans the stack — the same source compiles differently per platform.
  • Inlining means no frame is created at all; the compiler saw the call and replaced it with the body.

A strong closing line: the call stack is a LIFO region with O(1) push and pop, and a function call is just "push return address, jump; restore, pop, jump back."

This answer walks

Follow-ups they'll push on

  1. 01What lives in a stack frame and what lives on the heap?
  2. 02What happens on the stack during recursion?
  3. 03Where is the return address stored, and what happens if a function overwrites it?
  4. 04Why is the stack faster than the heap for allocation?

More interviews in this topic

One dispatch a week

The trace behind each question, the tradeoff that explains it, and one technical dispatch per week — no noise.

One technical dispatch per week. No noise.