1. Gyanastra
  2. Compiler Design
  3. Runtime Environments
  4. Static Storage Allocation Strategy
Free lesson, no account needed 7 min read 11 practice questions after it
Asked in past papers3 times
  • GATE CSE201420081989

Compiler Design · Runtime Environments

Static Storage Allocation Strategy

Overview

A runtime environment is the support a program receives while it runs, and a central part of that support is the storage allocation strategy — the policy deciding where and when memory is given to program elements (variables, functions).
There are three strategies: static, stack, and heap allocation. This section covers static allocation and then stack allocation.

Key concepts

  • Storage allocation strategy: the policy that decides where and when memory is given to program elements such as variables and functions.
  • Static allocation: allocation is done at compile time; every element must be specified at compile time itself.
    Bindings do not change at runtime — once a variable is bound to an address, it can never be moved out of that address. There is nothing like malloc and free; the element must stay there always.
    • Advantage: the element is there forever, till the end of the program.
    • Disadvantage: if the element is not needed in the middle of the program, it cannot be freed.
  • One activation record per procedure (static): at compile time you cannot decide how many times a function will be called, so the best you can do is allocate one activation record per function — with 10 functions, 10 activation records — assuming every function is called only once at any time.
    A call to a function uses that function's pre-allocated region.
  • Stack allocation: whenever an activation begins, an activation record is pushed onto the stack; whenever the activation ends, the record is popped off. The stack can grow and then shrink, so recursion is possible.
  • Fresh storage for locals (stack): local variables are bound to fresh storage on every call.
    If a function contains int x, that integer is created as many times as the function is called, each time in a different space in the stack, and each copy has a different value.

How it works

Static allocation

  1. All allocation is performed at compile time; every element must be specified then.
  2. Bindings are fixed: a variable bound to an address is never moved, and there is no malloc/free.
  3. Exactly one activation record is allocated per procedure, since the number of calls cannot be known at compile time.
  4. A call to a function must use only the space allocated for that function. If one activation of the function is already active, calling it again is not possible — there is no more space to run it.
  5. Because a function already occupying its space cannot be called again, recursion (a function calling itself) is not possible.

Stack allocation

  1. When a function is called, a new activation is created and its activation record is pushed onto the stack.
  2. Each call pushes one more record, so a function can be called once, then a second time, and so on — therefore recursion is possible.
  3. When the activation ends (the function is over), its activation record is popped off.
  4. Local variables are created again on every call, each in a different stack space with a different value.

Formulas

  • No formulas are given in this section.

Worked examples

Example 1 — Static allocation of activation records Program with functions F1, F2, F3, …, F10.

  • Static strategy: allocate exactly one activation record per function.
  • Layout: record for F1, record for F2, record for F3, record for F4, …, record for F10.
  • Call F1 → use only the F1 record. Call F1 again while the first call is active → impossible (no more space for F1).
  • Consequence: recursion (F calling F) cannot work.

Example 2 — Guessing two activation records Suppose two activation records are allocated for a function.

  • The function can be called two times (one active, then one more).
  • A third simultaneous call is not possible.

Example 3 — Space fixed at compile time At compile time you specify: 10 functions and 10 variables.

  • The OS allocates exactly the space for those 10 functions and 10 variables.
  • Requesting one more variable at runtime → no space, because the entire space has already been given away.

Example 4 — Counting calls with a local variable under stack allocation Code inside function F:

int count = 0;
count++;

and count is printed in main.

  • Call 1: a fresh count is created in the activation record, count = 0, incremented to 1. On return the activation record is popped; the value is lost.
  • Call 2: a new, independent count is created, count = 0, incremented to 1. It has no knowledge of the earlier variable whose value was 1.
  • Call 3: same again — count = 0, made to 1.
  • Result: the count never accumulates; the printed value does not reflect the number of calls.

Comparison

AspectStatic allocationStack allocation
When allocatedAt compile timeOn each function call, activation record pushed
Binding changes at runtimeNo — binding fixed foreverNew space for each call
malloc / freeNot availableNot available (grow/shrink by push/pop only)
Activation records per procedureExactly oneOne per call (new record each call)
RecursionNot supportedSupported
Size of data objectsMust be known at compile time—
Dynamic data structuresNot possible (no heap support)—
Local variable lifetimeAlive throughout the program (the one advantage)Lost when the activation is popped
Local variable storageFixedBound to fresh storage on every call

Revision summary

  • Static allocation happens entirely at compile time; bindings never change at runtime, and there is no malloc/free.
  • Static allocation gives exactly one activation record per procedure, because the number of calls cannot be guessed at compile time.
  • Recursion is impossible under static allocation: a function already sitting in its space cannot be called again.
  • Under static allocation, object sizes must be known at compile time — an array's size cannot be declared later.
  • No dynamic data structures under static allocation: the OS allocates exactly the space specified at compile time, so one more variable gets no space.
  • The only advantage of static allocation is that a variable stays alive throughout the program; this is not very advantageous for many functions, so static allocation is not very popular.
  • Stack allocation pushes a record on every call and pops it when the activation ends, so recursion is possible.
  • Stack allocation's disadvantage: local variables cannot retain values once the activation ends — the count example restarts at 0 and becomes 1 on every call.

Previous-year exam insights

  • Static allocation is possible only when no element's size or number of instances depends on runtime input: no recursion, no variable-size objects, and no dynamic data structures; the compiler must know all sizes and the maximum number of simultaneous activations.
    Asked inGATE 2014GATE 2008
  • Recursion requires that a procedure may have multiple simultaneously live activation records, so it cannot be implemented with static allocation of activation records; stack (or heap) allocation of activation records is what makes recursion possible.
    Asked inGATE 2014GATE 2008
  • Garbage collection is not needed for recursion; recursion only needs dynamic (stack) allocation of activation records, and neither heap nor garbage collection is essential for it.
    Asked inGATE 2014
  • A language with no nested procedures and no global variables needing dynamic extent can use static storage allocation even if it permits recursion only when each procedure has at most one live activation; access links/displays are needed only when procedures are nested.
    Asked inGATE 2008
  • Stack allocation supports recursion but not indefinite extent: a value whose lifetime must outlive the activation (e.g., returned pointer to a local, or data whose size is known only at runtime) must be placed on the heap.
  • Static allocation is the simplest and cheapest strategy (no runtime allocation overhead, addresses known at link time, direct absolute addressing), which is why it is used for globals, static/lexical variables, constants and program code.
Pages 1–2 of 2