Programming Search⌘K

Conceptcore

Stack frame

Also called activation record, activation frame, call frame, frame pointer, base pointer.

A stack frame is the entry on the call stack for one call: its arguments, its local variables, the return address and saved registers. A call makes one, and the return frees it.

Applies to
core-languages

Idea

Each call of a function needs its own place for its arguments and its local variables, and it must remember where to go back to. A stack frame is that place. A call puts a frame on top of the call stack, and the return removes it. When a function calls itself, each call has its own frame, so each call has its own copy of the locals.

import sys


def count(n):
    here = n * 10                    # a local: one copy in each frame
    print(f"enter n={n} here={here}")
    if n > 1:
        count(n - 1)
    else:
        frames = []
        f = sys._getframe()          # the frame of this call
        while f.f_code is count.__code__:
            frames.append(f"n={f.f_locals['n']}")
            f = f.f_back             # the frame of the caller
        print("frames:", " <- ".join(frames))
    print(f"leave n={n} here={here}")


count(3)
Output
enter n=3 here=30
enter n=2 here=20
enter n=1 here=10
frames: n=1 <- n=2 <- n=3
leave n=1 here=10
leave n=2 here=20
leave n=3 here=30

At the deepest point, three frames of count are on the stack, and each frame holds its own n and here. When the inner calls return, the outer frames still have their values, so leave n=3 prints here=30.

How it works

What a frame holds. A call stack "is composed of stack frames (also called activation records or activation frames)"1. "Each stack frame corresponds to an invocation of a subroutine that has not yet completed with a return"1. A frame typically holds "the arguments (parameter values) passed to the routine on the call stack (if any)", the return address to the caller, and "space for the local variables of the routine (if any)"1. GDB says the same: "The frame contains the arguments given to the function, the function's local variables, and the address at which the function is executing"2. Not every argument is in the frame: "if there are only a few small parameters, processor registers will be used to pass the values"1.

Made on call, freed on return. "Each time a function is called, a new frame is made. Each time a function returns, the frame for that function invocation is eliminated. If a function is recursive, there can be many frames for the same function"2. GDB numbers the frames from the top: the level "is zero for the innermost frame, one for the frame that called it, and so on upward"2.

Prologue and epilogue. The compiler adds code at the start and at the end of each function to make and free the frame. The prologue prepares "the stack and registers for use within the function", and the epilogue "restores the stack and registers to the state they were in before the function was called"3. On x86, a typical prologue saves the old frame pointer, copies the stack pointer into the frame pointer, and moves the stack pointer to make room: "the stack pointer is decreased to make room for the function's local variables"3.

Stack pointer and frame pointer. "A frame pointer of a given invocation of a function is a copy of the stack pointer as it was before the function was invoked"1. The fields of the frame can then be found "as negative offsets of the stack pointer" or "as positive offsets of the frame pointer"1. "In most systems a stack frame has a field to contain the previous value of the frame pointer register"1. These saved values link each frame to the frame of its caller, and this link "enables code to access each frame successively underneath" the frame of the running routine1. A debugger walks the stack in this way to print a backtrace.

A real layout. The Go ABI for amd64 gives a frame with, from the top down, the return PC, the saved RBP, the locals and the outgoing arguments4. "The stack pointer, RSP, grows down and is always aligned to 8 bytes"4. "On entry, a function subtracts from RSP to open its stack frame and saves the value of RBP directly below the return PC"4. In the Microsoft x64 convention, "A function's prolog is responsible for allocating stack space for local variables, saved registers, stack parameters, and register parameters"5.

Frames in a virtual machine. A virtual machine has frames too, but not the same fields. In the JVM, "Each frame has its own array of local variables (§2.6.1), its own operand stack (§2.6.2), and a reference to the run-time constant pool"6. "The sizes of the local variable array and the operand stack are determined at compile-time"6. In CPython, a frame is an object that the program can read. Its f_back attribute "Points to the previous stack frame (towards the caller)", and f_locals is "The mapping used by the frame to look up local variables"7. In JavaScript, MDN calls the frame an execution context: "An execution context, also known generally as a stack frame, is the smallest unit of execution"8.

In languages

LanguageVerdictNote
PythonNative
C++Native
JavaNative
CNative
C#Native
JavaScriptNative
TypeScriptNative
GoNative
RustNative
KotlinNativeKotlin on the JVM uses JVM frames, so each call has its own locals as in Java. No example file exists because kotlinc is not installed.
SQLDoes not applyA query has no frames that the code can see.
BashNative
PowerShellNativeEach call of a function runs in its own scope, so its variables are separate. No example file exists because pwsh is not installed.
HTMLDoes not applyHTML has no calls.
CSSDoes not applyCSS has no calls.
Regular expressionsDoes not applyA regular expression has no calls.
Language Verdict What the frame is and what the program sees
Python native a frame object with f_locals, f_back and f_code; sys._getframe() and inspect.currentframe()
JavaScript native an execution context for each call; the program cannot read it
TypeScript native as in JavaScript
Java native a JVM frame with a local variable array and an operand stack; the program cannot read the locals
Kotlin native JVM frames, as in Java
C# native the runtime chooses where locals go; a local that a lambda captures moves to an object on the heap13
Go native the compiler chooses which locals stay in the frame and which move to the heap
Rust native locals live in the frame of the call; a Box puts its value on the heap
C native the platform ABI sets the layout; GCC reports frame sizes with -fstack-usage
C++ native as in C; destructors of locals run when the frame is freed
Bash native local gives a variable for one call of a function
PowerShell native each call of a function gets its own scope for its variables
SQL n/a a query has no frames that the code can see
Regex n/a a pattern has no calls
HTML n/a HTML has no calls
CSS n/a CSS has no calls

The example files run the same recursion in each language, and each call prints its own copy of here after the inner calls return. Python also walks the frames with f_back. C, C++ and Rust also record the address of here in each call and show that the three frames have three different addresses. In Bash, local makes the variable "have a visible scope restricted to that function and its children"9, so each call has its own here.

PythonGitHub
import sys


def count(n):
    here = n * 10                    # a local: one copy in each frame
    print(f"enter n={n} here={here}")
    if n > 1:
        count(n - 1)
    else:
        frames = []
        f = sys._getframe()          # the frame of this call
        while f.f_code is count.__code__:
            frames.append(f"n={f.f_locals['n']}")
            f = f.f_back             # the frame of the caller
        print("frames:", " <- ".join(frames))
    print(f"leave n={n} here={here}")


count(3)
Output
enter n=3 here=30
enter n=2 here=20
enter n=1 here=10
frames: n=1 <- n=2 <- n=3
leave n=1 here=10
leave n=2 here=20
leave n=3 here=30
  • Verdict: native. Each call has a frame object. f_locals holds the locals of the call, and f_back is the frame of the caller.
#include <iostream>
#include <vector>

static std::vector<const int*> addrs; // the address of here in each call

struct Trace {
    int n;
    const int& here;
    ~Trace() { std::cout << "leave n=" << n << " here=" << here << "\n"; }
};

void count(int n) {
    const int here = n * 10; // a local: it lives in the frame of this call
    addrs.push_back(&here);
    Trace trace{n, here};    // its destructor runs when the call ends
    std::cout << "enter n=" << n << " here=" << here << "\n";
    if (n > 1) {
        count(n - 1);
    } else {
        bool distinct = addrs[0] != addrs[1] && addrs[1] != addrs[2] && addrs[0] != addrs[2];
        std::cout << addrs.size() << " frames, " << addrs.size() << " "
                  << (distinct ? "different" : "shared") << " addresses\n";
    }
}

int main() {
    count(3);
}
Output
enter n=3 here=30
enter n=2 here=20
enter n=1 here=10
3 frames, 3 different addresses
leave n=1 here=10
leave n=2 here=20
leave n=3 here=30
  • Verdict: native. Each call has a frame with its own n and here. When the call ends, the destructors of its locals run in the reverse order of their construction, and here ~Trace prints the leave line.
JavaGitHub
public class Main {
    static void count(int n) {
        int here = n * 10; // a local: one slot in the frame of each call
        System.out.println("enter n=" + n + " here=" + here);
        if (n > 1) {
            count(n - 1);
        }
        System.out.println("leave n=" + n + " here=" + here);
    }

    public static void main(String[] args) {
        count(3);
    }
}
Output
enter n=3 here=30
enter n=2 here=20
enter n=1 here=10
leave n=1 here=10
leave n=2 here=20
leave n=3 here=30
  • Verdict: native. Each call gets a JVM frame. n and here are slots in the local variable array of that frame.
  • The program cannot read the locals of a frame. StackWalker gives only the method and the line of each frame.
#include <stdio.h>

static const int *addrs[3]; /* the address of here in each call */
static int depth = 0;

static void count(int n) {
    int here = n * 10; /* a local: it lives in the frame of this call */
    addrs[depth++] = &here;
    printf("enter n=%d here=%d\n", n, here);
    if (n > 1) {
        count(n - 1);
    } else {
        int distinct = addrs[0] != addrs[1] && addrs[1] != addrs[2] && addrs[0] != addrs[2];
        printf("%d frames, %d %s addresses\n", depth, depth, distinct ? "different" : "shared");
    }
    printf("leave n=%d here=%d\n", n, here);
}

int main(void) {
    count(3);
    return 0;
}
Output
enter n=3 here=30
enter n=2 here=20
enter n=1 here=10
3 frames, 3 different addresses
leave n=1 here=10
leave n=2 here=20
leave n=3 here=30
  • Verdict: native. Each call has a frame with its own n and here. The platform ABI sets the layout of the frame, and the C standard does not describe it.
  • The saved addresses are only compared while all three calls are active. After count returns, a pointer to its here points into a dead frame.
using System;

static class Demo
{
    static void Count(int n)
    {
        int here = n * 10; // a local: one copy in each call
        Console.WriteLine($"enter n={n} here={here}");
        if (n > 1)
        {
            Count(n - 1);
        }
        Console.WriteLine($"leave n={n} here={here}");
    }

    static void Main()
    {
        Count(3);
    }
}
Output
enter n=3 here=30
enter n=2 here=20
enter n=1 here=10
leave n=1 here=10
leave n=2 here=20
leave n=3 here=30
  • Verdict: native. Each call has its own copy of n and here. Where the runtime keeps them is its own choice, so the program should depend only on the copy for each call.
  • A local that a lambda captures moves into a hidden object on the heap, so it lives after the call returns.
JavaScriptGitHub
function count(n) {
  const here = n * 10; // a local: one copy in each call
  console.log(`enter n=${n} here=${here}`);
  if (n > 1) {
    count(n - 1);
  }
  console.log(`leave n=${n} here=${here}`);
}

count(3);
Output
enter n=3 here=30
enter n=2 here=20
enter n=1 here=10
leave n=1 here=10
leave n=2 here=20
leave n=3 here=30
  • Verdict: native. Each call has its own execution context with its own n and here, so the inner calls do not change the values of the outer ones.
  • The program cannot read a frame. new Error().stack gives only the names and places of the calls, as text.
TypeScriptGitHub
function count(n: number): void {
  const here: number = n * 10; // a local: one copy in each call
  console.log(`enter n=${n} here=${here}`);
  if (n > 1) {
    count(n - 1);
  }
  console.log(`leave n=${n} here=${here}`);
}

count(3);
Output
enter n=3 here=30
enter n=2 here=20
enter n=1 here=10
leave n=1 here=10
leave n=2 here=20
leave n=3 here=30
  • Verdict: native. TypeScript compiles to JavaScript, so the frames are JavaScript execution contexts. The types add nothing to the frame.
package main

import "fmt"

func count(n int) {
	here := n * 10 // a local: one copy in each call
	fmt.Printf("enter n=%d here=%d\n", n, here)
	if n > 1 {
		count(n - 1)
	}
	fmt.Printf("leave n=%d here=%d\n", n, here)
}

func main() {
	count(3)
}
Output
enter n=3 here=30
enter n=2 here=20
enter n=1 here=10
leave n=1 here=10
leave n=2 here=20
leave n=3 here=30
  • Verdict: native. Each call has its own n and here. The compiler keeps a local in the frame when it can prove that nothing uses it after the return, and moves it to the heap if not.
  • The example does not print addresses: to take the address of here can change where the compiler puts it.
RustGitHub
fn count(n: i32, addrs: &mut Vec<*const i32>) {
    let here = n * 10; // a local: it lives in the frame of this call
    addrs.push(&here as *const i32);
    println!("enter n={} here={}", n, here);
    if n > 1 {
        count(n - 1, addrs);
    } else {
        let distinct = addrs[0] != addrs[1] && addrs[1] != addrs[2] && addrs[0] != addrs[2];
        let word = if distinct { "different" } else { "shared" };
        println!("{} frames, {} {} addresses", addrs.len(), addrs.len(), word);
    }
    println!("leave n={} here={}", n, here);
}

fn main() {
    let mut addrs = Vec::new();
    count(3, &mut addrs);
}
Output
enter n=3 here=30
enter n=2 here=20
enter n=1 here=10
3 frames, 3 different addresses
leave n=1 here=10
leave n=2 here=20
leave n=3 here=30
  • Verdict: native. Each call has its own frame, and here is in it. The three calls are on the stack at the same time, so the three copies of here have three addresses.
  • The raw pointers are only compared, never read. After a call returns, its frame is gone and the pointer to its here must not be used.
BashGitHub
count() {
  local n=$1
  local here=$((n * 10)) # local: one copy for each call
  echo "enter n=$n here=$here"
  if (( n > 1 )); then
    count $((n - 1))
  fi
  echo "leave n=$n here=$here"
}

count 3
Output
enter n=3 here=30
enter n=2 here=20
enter n=1 here=10
leave n=1 here=10
leave n=2 here=20
leave n=3 here=30
  • Verdict: native. local gives each call of count its own n and here. Without local, the variables would be global, and the inner calls would change the values that the outer calls print.
  • No frame pointer. A compiler can leave out the frame pointer and use only the stack pointer. GCC's -fomit-frame-pointer will "Omit the frame pointer in functions that don't need one", and "on many targets it also makes an extra register available"10. A debugger then needs other data to walk the stack. GDB says that "Some compilers provide a way to compile functions so that they operate without stack frames"2.
  • A frame that outlives its call. A frame can stay after its function stops for a time. MDN says that "When a frame is popped, it's not necessarily gone forever", as for a generator function8. A closure keeps the variables that it uses alive after the call returns. See closures.
  • A frame that is reused. With tail-call elimination, "Most of the frame of the current procedure is no longer needed, and can be replaced by the frame of the tail call"11.
  • A local that is not in the frame. In Go, "if the compiler cannot prove that the variable is not referenced after the function returns, then the compiler must allocate the variable on the garbage-collected heap to avoid dangling pointer errors"12.

Pitfalls

  • A pointer into a dead frame. In C and C++, a function that returns the address of a local returns a pointer to a frame that no longer exists. The memory is reused by the next call. See stack and heap.
  • Value types and the stack. It is often said that a C# value type lives on the stack. Eric Lippert, who worked on the C# compiler, calls this a "memory-model implementation detail I often see presented as a fact"13. He notes that an integer field of a class is part of the memory of the object, and that object is on the heap13. See value types and reference types.
  • Large locals. A large array as a local makes a large frame, and deep calls with large frames use up the stack sooner. See stack overflow.
  • Missing frames. An inlined function has no frame of its own, so it is not in a stack trace. See call stack.

Cost

To make a frame costs a few instructions: the prologue moves the stack pointer by the size of the frame, and the epilogue moves it back3. The size is known when the code is compiled. GCC can report it: -fstack-usage "Makes the compiler output stack usage information for the program, on a per-function basis"14, and -Wframe-larger-than= will "Warn if the size of a function frame exceeds byte-size"15. The warning is approximate, and "any space allocated via alloca, variable-length arrays, or related constructs is not included"15.

History

Languages with nested procedures, such as ALGOL 60, need a frame to find the frames of the procedures around it. One way is an array of pointers "known as a display which is indexed to locate a desired frame"1. Some machines did this in hardware: "the Burroughs B6500 implemented such a display in hardware which supported up to 32 levels of static nesting"1. In ALGOL 60 and PL/I, a block inside a procedure "may have its own local variables, allocated on block entry and freed on block exit"1. In 1977, Guy L. Steele argued that a call in tail position "can be best treated as a direct transfer of control to the called procedure, typically eliminating unnecessary stack manipulation operations"11.

References

  1. Call stack, Wikipedia, Structure; Stack and frame pointers; Storing the address to the caller's frame
  2. Stack Frames, Debugging with GDB, 8.1 Stack Frames
  3. Function prologue and epilogue, Wikipedia, Prologue; Epilogue
  4. Go internal ABI specification, amd64 architecture, Stack layout
  5. x64 stack usage, Microsoft C++ documentation, Stack allocation
  6. The Structure of the Java Virtual Machine, Java Virtual Machine Specification, Java SE 21, 2.6 Frames; 2.6.1 Local Variables; 2.6.2 Operand Stacks
  7. Data model, Python 3.14 documentation, 3.2.13.2 Frame objects
  8. JavaScript execution model, MDN Web Docs, Stack and execution contexts; Generators and reentry
  9. Bash Builtin Commands, Bash Reference Manual, local
  10. Options That Control Optimization, GCC documentation, -fomit-frame-pointer
  11. Tail call, Wikipedia, Introduction
  12. Frequently Asked Questions, The Go Programming Language, How do I know whether a variable is allocated on the heap or the stack?
  13. Eric Lippert, The Stack Is An Implementation Detail, Part One, Fabulous Adventures In Coding, 2009, Whole post
  14. GCC Developer Options, GCC documentation, -fstack-usage
  15. Options to Request or Suppress Warnings, GCC documentation, -Wframe-larger-than

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW4 complete
  • a review of the text: no review record reviews/SJGB98/text.yaml