What is the Runtime Environment?
The Runtime Environment refers to the organization of memory and support mechanisms
used when a program is executed. The compiler is responsible for generating code that
manages memory, handles function calls, variable access, etc., during runtime.
🧱 Memory Layout of a Running Program
When a program is running, its memory is divided into several segments:
sql
CopyEdit
+----------------------+
| Command-line args |
| Environment vars |
+----------------------+
| Stack (grows down) | ← Local variables, function calls
+----------------------+
| Heap (grows up) | ← Dynamically allocated memory
+----------------------+
| BSS Segment | ← Uninitialized global/static variables
+----------------------+
| Data Segment | ← Initialized global/static variables
+----------------------+
| Text Segment | ← Program code (instructions)
+----------------------+
🧩 Key Components of the Runtime Environment
1. Activation Record (Stack Frame)
When a function is called, an activation record is pushed onto the stack. It contains:
Parameters
Return address
Local variables
Temporary values
Control link (link to the caller’s activation record)
Activation record layout:
pgsql
CopyEdit
|--------------------|
| Return Value |
| Return Address |
| Control Link |
| Access Link (opt.) |
| Parameters |
| Local Variables |
| Temporaries |
|--------------------|
2. Stack
Supports function call and return.
Grows and shrinks dynamically.
Stores activation records.
3. Heap
Used for dynamic memory allocation (malloc in C, new in Java).
Managed at runtime.
Unstructured compared to the stack.
4. Static Area (Global Area)
Stores global and static variables.
Lifetime: entire duration of the program.
5. Code Segment (Text Segment)
Contains the actual machine code (instructions).
Read-only in most systems to prevent accidental modification.
An Activation Record (also called a Stack Frame) is a block of memory used to store
information about a single function call during program execution.
It is pushed onto the stack when a function is called and popped off when the function returns.
📦 Contents of an Activation Record
An activation record typically contains:
Field Description
Return Address Address to return to after the function ends.
Actual Parameters Values passed to the function.
Local Variables Variables declared inside the function.
Temporary Values Intermediate values needed during computation.
Control Link (Dynamic
Points to the caller’s activation record (previous stack frame).
Link)
Access Link (Static Link) Points to the activation record of the lexically enclosing function
(optional) (used in nested functions).
Return Value (optional) Space to store return value, if needed.
📊 Typical Layout of Activation Record (Top to Bottom):
|----------------------|
| Return Value | ← optional
|----------------------|
| Return Address |
|----------------------|
| Control Link |
|----------------------|
| Access Link | ← if using static scoping
|----------------------|
| Actual Parameters |
|----------------------|
| Local Variables |
|----------------------|
| Temporaries |
|----------------------|
Grows downward on most systems (stack grows down).
The layout may vary slightly depending on compiler and language features.
In compiler design and runtime systems, the heap is a region of memory used for dynamic
memory allocation.
Memory in the heap is manually managed by the programmer (e.g., using malloc in C
or new in C++/Java).
It is not automatically freed like the stack.
It is suitable for data whose lifetime is not tied to a function call.
🧠 Key Characteristics of Heap Memory:
Feature Description
Purpose Used for dynamic memory allocation.
Managed By Programmer (or garbage collector in some languages).
Lifetime Until explicitly freed or the program ends.
Growth Direction Grows upward in memory (opposite of the stack).
Allocation Slower than stack (due to bookkeeping and fragmentation).
🧱 Heap vs Stack (Comparison)
Feature Stack Heap
Allocation Type Static or automatic Dynamic (manual or GC)
Lifetime Ends with function call Manual control (or GC)
Allocation Speed Fast Slower
Size Limit Smaller Larger
Memory Management Compiler/runtime Programmer (or GC)
🔧 How is Heap Used?
1. In C / C++
c
CopyEdit
int* p = (int*) malloc(sizeof(int) * 10); // allocate array of 10 ints
*p = 100; // assign value
free(p); // deallocate memory
malloc allocates memory on the heap.
free deallocates it.
If you forget to free, it causes a memory leak.