Compiler Design · Runtime Environments
Overview of Runtime Environment and Process Memory Layout
Overview
The runtime environment is the support a program receives from the operating system when it runs: how the program is loaded and what support it needs during execution.
A running program becomes a process managed by the OS, and the OS gives every process a fixed process memory layout divided into machine code, static/global data, heap and stack.
Understanding this layout explains why array sizes must be fixed at compile time and why heap and stack grow toward each other.
Key concepts
- Runtime environment: the support a program needs from the operating system when it runs.
It answers two questions: how should a program be loaded into the operating system, and what support does the program need while it executes? - Process: a program in execution. Before running a program, the OS creates a process for it, and the process is managed by the OS (OS process management).
- Machine code (text/code segment): the bottom of the layout; the
.exe(Windows) ora.out(Linux) file itself is placed here, and it is nothing but machine code. - Static / global segment: placed above the machine code.
It holdsstaticvariables (created once, permanent storage, referable any number of times for the entire lifetime of the program) and all global variables, including global and static arrays; local arrays live on the stack and dynamically allocated arrays on the heap. - Heap: used for dynamic memory allocation.
mallocandcalloccreate new variables or data structures in heap memory; the heap grows upwards with no hard and strict boundary. - Stack: required for function calling. Every function call pushes an activation record on the recursion stack; the stack grows downwards.
- Three storage techniques: every present-day operating system provides static, heap and stack storage.
Initially only one of them was used; later it was found that using all three is not just efficient but necessary nowadays for functions.
How it works
- Write a C program, compile it, and get an output file (
.exeon Windows,a.outon Linux). - Execute the file; the OS creates the process and its memory layout.
- The machine code of the file is placed at the bottom.
- Above it lies the static/global segment:
staticvariables, global variables and arrays.
Because the compiler must reserve a fixed amount of storage for an array (on the stack for locals, in the static area for globals), its size must be known at compile time. - Above the static area the heap starts; each
malloc/callocallocates there, and the heap grows upwards as and when required. - From the top, the stack grows downwards; each function call pushes an activation record.
- Heap and stack grow in opposite directions with no strict boundary: whichever needs more takes more.
If the space at the meeting end is exhausted, a memory error / segmentation error occurs (the exact message depends on the operating system).
Formulas
-
Layout order (bottom to top): machine code static/global heap (grows up) stack (grows down)
-
Notebook split, Strategy A: TOC gets pages, compiler design gets pages, where is the total number of pages
-
Notebook split, Strategy B: TOC grows from the front, compiler design from the back; pages are wasted only if total demand exceeds
-
Process Memory Layout
Drawing the diagram…
- Shows the process memory layout from bottom to top with heap growing up and stack growing down toward each other.
Worked examples
Process memory layout of a compiled C program
- Compile a C program →
a.out(Linux) or.exe(Windows). - Execute it → the OS creates the process.
- The OS places the machine code of
a.outat the bottom of the process memory. - Above the machine code it places the static/global area: any
staticvariable, any global variable, and any array (arrays are static by default, so their size must be given at compile time). - Above that, the heap starts; each
malloc/calloccall creates data in the heap, and the heap grows upward. - From the top, the stack grows downward; each function call pushes an activation record.
- Heap and stack grow toward each other with no fixed boundary; whichever needs more takes more. If the space runs out, the OS reports a memory/segmentation error.
Notebook analogy (numerical reasoning)
- Suppose the notebook has pages. Strategy A: give pages to the Table of Contents (TOC) and to compiler design notes.
If TOC needs more than while compiler design uses less than , the extra compiler pages cannot be used by TOC, because its pages must be contiguous → wastage. - Strategy B: write TOC from one side of the notebook and compiler design from the other side (turn the book around and start from the last page).
Each gets whatever it needs, up to the whole book, and pages are wasted only if the total demand exceeds . - Same reasoning applies to heap and stack: we cannot predict which will need more, so a fixed split wastes memory; opposite growth from the two ends is better.
Comparison
| Segment | What it holds | Growth direction | Size known when? |
|---|---|---|---|
| Machine code | The .exe / a.out machine code | Fixed at bottom | At load time |
| Static / global | static variables, global variables, arrays (static by default) | Fixed | Must be fixed at compile time (array size must be given) |
| Heap | Dynamically allocated data (malloc, calloc) | Grows upwards | Grows as needed, no strict boundary |
| Stack | Activation records of function calls (recursion stack) | Grows downwards | Grows as needed, no strict boundary |
Revision summary
- A running program is a process; the OS creates the process before the program runs and manages it.
- Arrays are static by default, so an array cannot be declared without a size — the size must be fixed at compile time so the OS knows how much to allocate before execution.
staticvariables and global variables have permanent storage for the entire lifetime of the program.malloc/callocallocate in the heap (grows up); function calls push activation records on the stack (grows down).- There is no strict boundary between heap and stack; a fixed split would waste memory because pages must be contiguous and we cannot predict which side needs more.
- Exhausting the space gives a memory/segmentation error; the space reserved per process is fixed by the OS.
- Every modern OS provides three storage techniques — static, heap, stack; initially only one was used, and using all three became necessary for functions.
- Know each strategy's advantages and disadvantages, and what would happen to a program if only one of static, heap or stack were provided.
Previous-year exam insights
- An activation record (stack frame) typically contains, in order: actual parameters, return value, control link (dynamic link) to the caller's activation record, access link (static link) to the enclosing scope's activation record, saved machine state/registers, and local data plus temporaries.
Asked inGATE 1996GATE 1998 - The sequence of procedure calls corresponds to a preorder traversal of the activation tree, and the sequence of procedure returns corresponds to a postorder traversal.
Asked inGATE 2021GATE 2023 - An expression with no embedded function calls can be evaluated with a single stack; nested/embedded calls require one activation record per active call, i.e., stack depth equal to the height of the call/expression tree.
Asked inGATE 2002 - A display is an array of pointers to activation records of the currently active procedures, used to access non-local variables in instead of following a chain of access links.
Asked inGATE 1998 - Dynamic memory allocation (heap allocation via malloc/new) happens at run time, not during compilation; type checking, symbol-table management and inline expansion are compile-time activities.
Asked inGATE 2014 - Parameter passing semantics: call by value copies the value; call by reference passes the address so the callee's assignments affect the caller; call by name textually substitutes the argument (re-evaluated each use); call by value-result copies in and copies back on return.
Under dynamic scoping, a non-local name resolves to the most recent active binding at run time, not the lexically enclosing one.
Asked inGATE 1989GATE 1995GATE 2003GATE 2004GATE 2007GATE 2016