Sub topic 1
How a Running Program Uses Memory
This chapter explains how a running program organizes its memory. It covers activation records, scope, parameter passing, heap allocation, and the intermediate code a compiler builds before it emits machine code.
Prerequisites
You should be able to read a short C program with functions and recursion, and you should know stacks and parse trees. The chapter assumes you can trace a recursive call by hand.
Storage Regions
A running program uses four regions of memory: code, static data, a stack, and a heap. The code region holds instructions. Static data holds globals that exist for the whole run. The stack holds one activation record per active call. The heap holds memory requested at run time.
Activation Records and the Control Stack
An activation record is the block of memory for one call. It stores parameters, the return address, local variables, and saved registers. The control stack pushes a record on each call and pops it on return.
int fact(int n) {
if (n <= 1) return 1;
return n * fact(n - 1);
}
int main(void) {
return fact(3);
}Calling Sequence and Frame Layout
The caller evaluates the arguments, places them in the callee's frame, and jumps to the callee. The callee saves registers, allocates locals, runs, places its result, and restores state. The frame layout fixes the offset of each field so that caller and callee agree on where each value lives.
Scope and Access
Static scope resolves a name by the program text: the nearest enclosing declaration wins. Dynamic scope resolves a name by the call chain: the most recent active declaration wins. Static scope uses access links, which are pointers from a frame to its lexically enclosing frame. A display is an array of such pointers indexed by nesting depth, which makes lookup faster.
Parameter Passing
- Call by value copies the argument. Callee changes do not reach the caller.
- Call by reference passes the address. Callee changes are visible to the caller.
- Copy-restore copies the value in, works on the copy, and copies it back on return.
- Call by name re-evaluates the argument expression at each use.
Heap Allocation and Garbage Collection
Heap blocks outlive the call that created them. Garbage collection reclaims blocks that the stack, globals, and registers can no longer reach. Reference counting is an alternative, but it cannot reclaim cyclic structures.
Why Compilers Use Intermediate Code
An intermediate representation (IR) is a form between source code and machine code. One IR lets a single optimizer serve many source languages and many target machines.
Syntax Trees and DAGs
A syntax tree shows the structure of an expression. In a + b c, the multiplication sits below the addition. A DAG (directed acyclic graph) merges identical subtrees. In a b + a b, the DAG has one node for a b with two parents, so the product is computed once.
Exercise
In the fact example, how many activation records are on the stack when fact(1) runs?
Show answer
Four: main, fact(3), fact(2), and fact(1).