How a running program uses memory: static, stack and heap allocation, activation records and activation trees, static vs dynamic scoping, access links for nested procedures, parameter passing and garbage collection, with the program-output questions GATE asks.
Activation records are pushed on a call and popped on return.
Concept notes
1. Storage organisation and allocation strategies
At run time a program's memory is divided into:
Code: the machine instructions, fixed size, known at compile time.
Static data: global and static variables, whose addresses are fixed at compile time.
Stack: one activation record per live procedure call, pushed on call and popped on return.
Heap: data whose lifetime is not tied to a call, allocated by malloc/new and freed explicitly or by a garbage collector.
The stack and heap grow towards each other into the free area between them.
Strategy
Where names are bound
Supports recursion?
Supports data outliving its call?
Static
fixed addresses at compile time
No (one copy per local)
Yes, values persist between calls
Stack
in the activation record
Yes
No
Heap
anywhere, any order
Yes
Yes
Static allocation (as in early Fortran) needs no run-time support but cannot handle recursion, since every call of a procedure would share the same locals. It also cannot allocate data whose size is known only at run time.
Stack allocation handles recursion naturally. Its danger is the dangling reference: a pointer to a local variable used after the procedure returns, when that record has been popped.
Heap allocation is needed when data must outlive the call that created it (linked structures, closures returned from functions). Forgetting to free heap memory causes a memory leak; freeing it too early creates a dangling pointer.
Run-time memory layout (left) and the usual fields of an activation record on the stack (right)
2. Activation records and activation trees
An activation is one execution of a procedure. Its activation record (frame) typically holds:
actual parameters and the returned value,
the control link (dynamic link): pointer to the caller's record,
the access link (static link): pointer to the record of the lexically enclosing procedure, needed for non-local names in nested procedures,
saved machine status (return address, saved registers),
local data and temporaries.
The calling sequence splits work between caller and callee: the caller evaluates arguments and stores the return address; the callee saves registers and initialises locals. The return sequence restores them in reverse.
Activation tree. Each node is an activation; its children are the calls it makes, in order. Then:
the number of activation records created = number of nodes in the tree,
the sequence of calls = preorder, the sequence of returns = postorder,
the control stack at any moment holds the path from the root to the currently running activation,
the maximum stack depth (records alive at once) = height of the tree in nodes.
For with base cases , the number of calls satisfies , , so , , . The maximum depth for is records (the chain ).
Activation records may also hold variable-length data (arrays sized at run time) placed after the fixed fields, reached through a pointer stored in the fixed part.
3. Static vs dynamic scoping
Scope rules decide which declaration a use of a name refers to.
Static (lexical) scoping: look in the current block, then in the block that textually encloses it in the source, outwards to the globals. Decided at compile time. Used by C, Java, Pascal.
Dynamic scoping: look in the current procedure, then in its caller, then the caller's caller, along the control stack. Decided at run time. Used by early Lisp and some shell scripts.
How to solve an output question.
For static scoping, for each non-local name, find the nearest enclosing declaration in the program text. Calls do not matter.
For dynamic scoping, at the moment of use, walk the call chain backwards and take the most recent activation that declares the name.
An assignment to a non-local name changes whichever variable that rule picks.
Example.
int x = 1;
void show() { print(x); }
void change() { int x = 5; show(); }
main() { change(); show(); }
Static: show is not nested in change, so its x is always the global: output 1 1. Dynamic: the first show is called from change, whose local x = 5 is the most recent: output 5 1.
Implementation. Static scoping with nested procedures uses access links or a display. Dynamic scoping uses the control links: deep access searches the stack; shallow access keeps one current value per name and saves and restores it on entry and exit.
4. Nested procedures: access links and displays
In languages with nested procedures (Pascal, ML), a procedure can use variables of the procedures that enclose it. The nesting depth of the main program is 1, a procedure declared inside it has depth 2, one declared inside that has depth 3, and so on.
Access links. Each record stores a pointer to the most recent record of its lexically enclosing procedure. If a procedure at depth uses a name declared at depth (), follow
access links from the current record, then use the known offset of the name in that record.
Setting the access link on a call from (depth ) to (depth ):
( is declared inside ): 's access link points to 's record.
: follow access links from 's record, and use that record.
Display. An array where points to the most recent active record at depth . A non-local at depth is reached in one step through , whatever the nesting difference. On entry to a procedure at depth , the old is saved in the new record and restored on exit.
Access links
Display
Cost of a non-local access
link hops
constant
Cost of a call
find the right link
save and restore one entry
Control links follow the calling order; access links follow the textual nesting. Mixing them up is the usual mistake.
5. Parameter passing and heap management
Mechanism
What the callee gets
Effect on the caller's variable
Call by value
a copy of the value
none
Call by reference
the address (l-value)
every change is seen at once
Call by value-result (copy-in copy-out)
a copy; copied back on return
changes appear only on return
Call by name
the argument text, re-evaluated at each use (as a thunk)
like textual substitution
Differences show up with aliasing, when the same variable is passed twice. For p(x, y) { x = x + 1; y = y + x; } called as p(a, a) with a = 2:
value: a stays 2;
reference: x and y are both a: , then ;
value-result: locals become , ; copying back then leaves (the order of copy-back matters).
Call by name differs from reference when the argument is an expression such as A[i] and i changes inside the procedure: each use re-evaluates A[i] with the current i.
Garbage collection reclaims heap blocks that are no longer reachable from the roots (globals, stack, registers).
Reference counting: each block counts pointers to it; free it when the count reaches 0. Reclaims garbage immediately, but cannot reclaim cycles and costs time on every pointer update.
Mark-and-sweep: mark everything reachable from the roots, then sweep and free the unmarked blocks. Handles cycles; pauses the program.
Copying collectors move live data to a fresh area, which also compacts it and removes fragmentation.
Explicit free risks dangling pointers (freeing too early) and leaks (never freeing).
Formulas to remember
Access links to followUse at depth of a name declared at depth
Access link of a calleeLinks to follow from the caller (depth ) when the callee has depth
Calls of a two-branch recursionWith ; equals for Fibonacci numbers
Records alive at once
Common mistakes
Using the call chain under static scoping; static scope depends only on where the procedure is written.
Confusing the control link (points to the caller) with the access link (points to the lexically enclosing procedure).
Counting the maximum stack depth as the total number of calls; depth is the longest root-to-leaf path in the activation tree.
Forgetting that call by value-result copies back on return, so the answer depends on the copy-back order when a variable is passed twice.
Assuming reference counting reclaims all garbage; cyclic structures are never freed by it.
Worked examples
Try each step yourself, then reveal the working.
Example 1: Static vs dynamic scoping output
conceptual
int x = 1;
void show() { print(x); }
void change() { int x = 5; show(); }
main() { change(); show(); }
What is printed under static scoping and under dynamic scoping?
Step 1: Static scoping: where is `show` written?
Try it on paper first.
Example 2: One call, three passing modes
numerical
void p(int x, int y) { x = x + 1; y = y + x; }
main() { int a = 2; p(a, a); print(a); }
What is printed with call by value, call by reference, and call by value-result (copy back first, then )?
Step 1: Call by value.
Try it on paper first.
Example 3: Counting activation records
numerical
int f(int n) { if (n <= 1) return n; return f(n-1) + f(n-2); } is called as f(5). How many activation records of f are created in total, and what is the largest number alive at the same time?
Step 1: Set up the call count.
Try it on paper first.
Example 4: Following access links
conceptual
main (depth 1) declares x and procedure p; p (depth 2) declares q; q (depth 3) declares r (depth 4). While r runs, how many access links are followed to reach x, and to reach a local of p? With a display, how many?
Step 1: Apply $n_p - n_x$ for `x`.
Try it on paper first.
Previous year questions
3 official GATE CSE questions on this topic, newest first.
10 questions: up to 3 official PYQs and original practice questions, with GATE marking (MCQ wrong answers lose marks). Score 60% or more to pass. A new set is drawn each time.
Mark as mastered
Loading your progress…
My revision notes
Notes written by the GATEiit team against the official GATE CSE syllabus; being checked by a reviewer. Updated 2026-11-21. Spotted a mistake? Use Report an issue on any question, or contact us.