What is Stack frame?
The area reserved on the stack each time a function is called. It holds the return address, saved registers and local variables and is freed when the function returns.
Japanese name: スタックフレーム
The area reserved on the stack each time a function is called. It holds the return address, saved registers and local variables and is freed when the function returns.
Japanese name: スタックフレーム
A frame is the slice of stack a single function call owns. Depending on the function it contains the return address, the old values of callee-saved registers the function intends to use, local variables that do not fit in registers or whose address is taken, spill slots for temporaries, and arguments beyond those passed in registers; An optional frame pointer register, s0 in RISC-V, can mark a fixed reference point in the frame. Because every call gets a fresh frame, each recursive invocation has its own independent copy of the locals.
By the usual convention the stack grows toward lower addresses, so a function allocates its frame by subtracting from the stack pointer. A typical RISC-V prologue is addi sp, sp, -16 followed by sw ra, 12(sp) and sw s0, 8(sp). The matching epilogue reloads s0 and ra, executes addi sp, sp, 16 and returns with ret. The standard RISC-V calling convention keeps sp aligned to 16 bytes, which is why small frames are rounded up to 16. Allocation and release cost only an add, which is why stack storage is far cheaper than heap allocation.
In RISC-V, a call made with jal writes the return address into register ra, so a function that calls others must save ra in its frame before the first call. A leaf function, which calls nothing, never overwrites ra and can return without saving it; if it also uses only caller-saved registers and keeps its locals in registers, it needs no frame at all. On x86 the situation differs: the call instruction always pushes the return address onto the stack, so every call touches memory. Tail-call optimization turns a final call into a jump that reuses the current frame, so deep tail recursion does not grow the stack.
Frame contents live only until the function returns, while heap objects persist until freed. Returning a pointer to a local variable is therefore a bug: the memory belongs to whatever frame is pushed next. Unbounded or very deep recursion exhausts the stack, whose size per thread is fixed and commonly a few megabytes or less, causing a stack overflow. Writing past the end of a local array can overwrite the saved return address, a classic security vulnerability; compilers counter it with stack canaries, guard values checked in the epilogue before returning.
Answer: C. Because it calls no one, ra is never overwritten and the return address stays intact
Every jal overwrites ra, so a function that calls others must first save its own return address on the stack. A leaf function makes no calls that write ra (jal ra or jalr ra), so ra stays intact and it can ret without saving it. If it also avoids callee-saved registers, it does not even need a stack frame. x86's call instruction always pushes the return address on the stack, which is a difference from RISC-V.
Answer: A. To grow the stack toward lower addresses and reserve a 16-byte work area
By the convention of many ISAs, RISC-V included, the stack grows from higher toward lower addresses. Subtracting 16 from sp makes the 16 bytes between the new sp and the old one this function's stack frame, where the return address, saved registers and local variables can be kept. At the end, "addi sp, sp, 16" restores sp and frees the area, returning to the state before the call.
The stack pointer marks the current top of the stack and may move during a function. The frame pointer, when used, holds a fixed address inside the current frame so locals and saved registers can be found at constant offsets, which also helps debuggers walk the call chain.
Each recursive call allocates a new stack frame, and the stack has a fixed maximum size. If the recursion is deep enough, the frames exceed that space; tail-call optimization or rewriting the recursion as a loop avoids the growth.
The local lives in the function's stack frame, which is released on return and reused by later calls. The pointer then refers to memory whose contents can change at any time.
Stack frame appears in 3 practice questions across 1 topic, including ISA & Assembly.
As an Amazon Associate I earn from qualifying purchases.
This page was drafted with AI and fact-checked by a separate AI review before publication. Our Editorial policy explains how we write and check content. If you spot an error, please tell us on our Contact page.