GATE / CS ARCHIVE

GATE CSE 1999 / QUESTION 16

Draw activation stacks and static links

Compiler Design · Runtime Environments & Parameter Passing · Full GATE CSE 1999 paper

GATE 1999, Q16 5 marks · Descriptive
Report issue

Runtime Environments & Parameter Passing

Question 16, GATE 1999: In a Pascal skeleton, program sort declares a,x and the sibling procedures readarray, exchange, qsort. qsort declares k,v and a nested function partition, which declares i,j. The active call sequence is sort → qsort(1,9) → qsort(1,3) → partition(1,3) → exchange(1,3). Draw the runtime stack with access links after each activation.
Tap to enlarge
Answer & solution

Answer: Both qsort frames and exchange link statically to sort; partition links to qsort(1,3). (no official key was published)

Access (static) links follow lexical nesting, whereas control (dynamic) links follow the caller. Label frames S=sort, Q1=qsort(1,9), Q2=qsort(1,3), P=partition(1,3), E=exchange(1,3). Each line below is a complete snapshot written bottom → top; the parenthesized target is the access link:

1. [S(outer/null)]
2. [S(outer/null)] [Q1(S)]
3. [S(outer/null)] [Q1(S)] [Q2(S)]
4. [S(outer/null)] [Q1(S)] [Q2(S)] [P(Q2)]
5. [S(outer/null)] [Q1(S)] [Q2(S)] [P(Q2)] [E(S)]

The final stack, top first, is:
E : static→S; dynamic→P
P : static→Q2; dynamic→Q2
Q2: static→S; dynamic→Q1
Q1: static→S; dynamic→S
S : static→outer/null; dynamic→outer/null

Recursive Q2 is not lexically declared inside Q1, so its static link must not point to Q1. Partition accesses its enclosing qsort variables such as v through Q2, then global a through Q2→S. Exchange was declared directly inside sort, so its a and x references use E→S, even though partition called it. readarray is not active and has no frame in these snapshots. Return addresses and saved registers may also occupy frames but do not change the links.

Original paper, page 8