liveBook · Manning

Stack frame

As explained in Rust in Action: Systems programming concepts and techniques

A stack frame is a per-function-call memory record containing call state, arguments, local variables, and return information.

A stack frame is the memory record created for one function call. It holds the function’s call state, arguments, return information, and local variables, except data allocated on the heap. section 6.3.1

Function calls create frames in Last In, First Out order. The stack pointer tracks the current frame: it moves as calls add frames and moves back when functions return. While an inner function runs, the caller’s frame is effectively frozen. section 6.3.1

A function call

Calling is_strong("justok") creates a frame for is_strong, including its password argument and return information. When the function returns whether the password is longer than five characters, that frame is removed and execution continues in the caller. section 6.3.1

Keep these differences from the dinner-plate analogy in mind:

  • The stack contains both frames and data. section 6.3.1
  • Programs can access multiple stack elements, not only the newest one. section 6.3.1
  • Frames can have different sizes rather than sharing one standard size. section 6.3.1
fn is_strong(password: String) -> bool {
    password.len() > 5
}

Use the function definition and its call to visualize a frame being created for a password-checking operation. section 6.3.1

During exception handling, the call stack is traversed in reverse through its frames. This process, called stack unwinding, invokes a personality function for each frame to determine whether it can handle the exception. section 11.4.1

How Rust in Action: Systems programming concepts and techniques covers it

How deep it goes
Explains frame contents, LIFO behavior, stack-pointer movement, and unwinding, but stops short of frame-layout implementation details.
Examples
Uses a dinner-plate comparison, a Rust password-checking function, and FledgeOS exception unwinding.
What you can do after
Evaluate and understand how calls, returns, and exception unwinding move through function-specific memory records.
How it connects
Connects stack frames to the stack, heap, function calls, stack pointers, call stacks, exception handling, and stack unwinding.

Also explained in

Compare how 3 books explain stack frame →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Rust in Action: Systems programming concepts and techniques. The section links point to the parts of the book it draws on.