Back to the 2023 paper

Module 3: Semantic Analysis, Symbol Table, Run-time environment

20237m

What is an activation record? Explain how they are used to access various local and global variables.

Worked SolutionAI Assisted

What is an activation record? How are local/global variables accessed?

Activation record (AR) — also called a stack frame — is a block of memory pushed onto the run-time stack for each call of a procedure/function. It holds everything needed to return from the call and to run the callee. When the procedure returns, its record is popped (unless it is kept for certain closures).

Typical layout (higher addresses → lower):

   [ actual parameters ]
   [ return value (if any) ]
   [ access link / control link (saved frame pointer) ]
   [ return address (saved PC) ]
   [ saved machine status ]
   [ local data (locals, temporaries) ]
   [ saved top-of-stack pointer ]

Accessing local variables:

  • While a procedure is active, the runtime keeps a base pointer (frame pointer) that names the bottom of the current AR.
  • Every local variable is stored at a fixed offset from the start of the AR, an offset chosen at compile time.
  • So x is accessed as base + offset(x) — no runtime search: just load [base+off].

Accessing global (non-local) variables:

  • Globals live in a global/static data area (not on the stack in most compilers). Each global has a fixed compile-time address/offset from a global base register/area, so access is again just an absolute/relative memory load.
  • Some compilers (old Pascal-style) used an access link/display to reach enclosing non-locals: each frame holds a pointer to the frame of the statically enclosing procedure; accessing a nonlocal of depth dd follows dd links. (Dynamic scoping instead uses a dynamic link up the call chain.)

Example (C):

int g = 10;                 /* global */
int f(int p) { int l; l = g + p; return l; }

At run time while inside f:

  • p and l are at FP + offset_p and FP + offset_l (local, in the AR).
  • g is at its fixed global-area address (single load).

Conclusion

  • Locals → offsets within the current activation record on the run-time stack (FP + offset).
  • Globals → fixed addresses in a global data region, known at compile time.

Similar questions

Official AppAndroid App

PYQDeck for Android

Solve BEU previous-year exam papers anywhere, anytime. Fast search, offline paper viewing, and step-by-step solutions designed specifically for your phone.