Sub topic 2
Activation records and the control stack
This chapter assumes you can read C-style functions and recursion, and that you know how a stack works. It covers what a compiler stores for each procedure call, how those records pile up on the control stack, and how to draw the stack for a recursive call. Later chapters cover the calling sequence in detail, access to non-local names, and parameter passing.
Why each call needs its own record
A procedure call is an activation. Each activation has its own local variables, its own parameters, and its own place to return to. If fact(3) calls fact(2), both activations use a variable called n, but the two copies hold different values. The compiler therefore cannot give n one fixed address in static storage. It reserves space for each activation when the call happens and releases it when the call returns.
That space is the activation record, also called a frame. Because calls and returns nest (the last procedure called is the first to return), the records live on a stack. This stack is the control stack, or run-time stack.
What an activation record contains
Layouts differ between compilers and machines. The textbook layout has these fields, listed here roughly from the caller's side to the callee's side:
| Field | Purpose |
|---|---|
| Actual parameters | Values or addresses the caller passes to the callee |
| Returned value | Space where the callee leaves its result for the caller |
| Control link | Pointer to the caller's activation record |
| Access link | Pointer to the record that holds non-local data (needed for nested procedures) |
| Saved machine status | Return address and saved register values |
| Local data | Variables declared inside the procedure |
| Temporaries | Intermediate values from expression evaluation |
Two of these fields are often confused:
- The control link points to the caller's frame. It lets the callee pop its own frame and restore the caller's context on return.
- The access link points to the frame of the procedure that lexically encloses this one. It is used to find non-local variables. For a language such as C with no nested procedures, it is not needed.
The control stack during a call
The stack holds one frame for each active procedure. An activation is active if it has been called and has not yet returned. When a procedure is called, its frame is pushed. When it returns, its frame is popped. The stack top is the currently running procedure.
Consider the factorial program.
int fact(int n) {
if (n <= 1) return 1;
return n * fact(n - 1);
}
int main(void) {
return fact(3);
}main calls fact(3). That call calls fact(2), which calls fact(1). The call fact(1) meets the base case and returns without a further call. The stack now holds four frames: main, fact(3), fact(2) and fact(1). This is its deepest point. Each fact frame has its own copy of n.
The solid arrows show the order of the calls. The dotted arrows show the control links, which point back toward the caller.
Activation tree
The activation tree records the history of calls. Each node is one activation. The root is the first activation (for example main). A node's children are the activations it called directly, left to right in the order of the calls. The control stack at any moment holds the path from the root to the currently running activation.
For fact(3) the tree is a single chain, because each activation makes at most one call:
main
└─ fact(3)
└─ fact(2)
└─ fact(1)A procedure that makes two calls gives a branching tree. For f(n) that calls f(n-1) and f(n-2) (Fibonacci), the tree for f(3) is:
f(3)
├─ f(2)
│ ├─ f(1)
│ └─ f(0)
└─ f(1)The tree has five nodes, but the stack never holds more than three frames at once: for example, f(3), f(2) and f(1) while the first leaf runs. When f(1) returns, it is popped, and f(0) is pushed in its place.
The rules that follow from this:
- Two activations are either nested (one is a descendant of the other) or disjoint in time. They never partially overlap.
- The maximum stack depth equals the longest root-to-leaf path in the tree, counted in nodes.
- The total number of activations equals the number of nodes in the tree.
Tracing stack depth in code
You can check depth reasoning by instrumenting a recursive function. This Python program prints the depth and the argument on each call and each return.
def fact(n, depth=1):
print(" " * depth + f"call fact({n}), depth {depth}")
result = 1 if n <= 1 else n * fact(n - 1, depth + 1)
print(" " * depth + f"return {result} from fact({n})")
return result
fact(3)The output shows fact(3) opening first and closing last, which is the last-in, first-out behavior of the control stack. Counting main, the C program above reaches one more frame than the largest depth printed here.
Common errors
- Counting
main. If the question draws the stack for the whole program, include the frame formain. If it asks only about the recursive function, do not. - Counting calls instead of depth. The Fibonacci example has five activations but a maximum depth of three.
- Mixing up the two links. The control link follows the dynamic order of calls. The access link follows the nesting in the source text. In a recursive chain the two can point to different frames.
- Assuming locals persist. A frame is gone after the return. Returning the address of a local variable leaves a dangling pointer, because the space is reused by the next call.
- Ignoring stack overflow. Unbounded recursion keeps pushing frames until the stack's memory runs out. A missing or unreachable base case causes this.
Exercise
A procedure g(n) calls g(n-1) once if n > 0, and then calls g(n-1) a second time if n > 1. Called from main as g(2), what is the maximum number of frames on the control stack, counting main, and how many activations of g occur in total?
Show answer
The activation tree for g(2) has root g(2) with two children g(1) and g(1). Each g(1) has one child g(0), and the second call to g(0)'s parent is skipped because n > 1 is false. So the nodes are g(2), g(1), g(0), g(1), g(0), which is 5 activations of g. The longest root-to-leaf path is g(2), g(1), g(0), which is 3 nodes. Adding the frame for main gives a maximum of 4 frames.
Exercise
Which field of an activation record lets a callee restore the caller's frame on return, and which field is used to reach a variable declared in an enclosing procedure?
Show answer
The control link points to the caller's frame and is used on return. The access link points to the frame of the lexically enclosing procedure and is used for non-local variables.