
This topic belongs to the Runtime Environment / Code Generation phase of a compiler. It explains how memory is organized when a program runs and how space...
This topic belongs to the Runtime Environment / Code Generation phase of a compiler. It explains how memory is organized when a program runs and how space is allocated for functions, variables, and procedures.
Storage organization refers to how memory is arranged and managed during program execution.
π It divides memory into different regions to store:
A typical program memory is divided into 4 main sections:
+----------------------+
| Text Segment | β Program code (instructions)
+----------------------+
| Static / Data Area | β Global & static variables
+----------------------+
| Heap | β Dynamic memory (malloc/new)
+----------------------+
| Stack | β Function calls, local variables
+----------------------+
Example:
malloc(), new
Stack allocation is a method of allocating memory for function calls in a Last In First Out (LIFO) manner using a stack.
π Each function call gets a block called:
Activation Record (Stack Frame)
An activation record is a block of memory used to store information about a function call.
+----------------------+
| Parameters |
+----------------------+
| Return Address |
+----------------------+
| Control Link |
+----------------------+
| Access Link |
+----------------------+
| Local Variables |
+----------------------+
| Temporary Values |
+----------------------+
Memory block (activation record) is created
New frame is pushed
Function executes using its frame
Frame is popped from stack
| Function C |
| Function B |
| Function A | β top of stack
When C finishes:
| Function B |
| Function A |
int main() {
int x = 10;
fun();
}
void fun() {
int y = 20;
}
main() activation record createdfun() called β new activation record pushedfun() completes β poppedmain()β Fast allocation and deallocation β Follows LIFO order β Memory reused automatically β Used for function calls β Efficient for recursive programs
β Cannot handle dynamic data (fully flexible structures) β Limited lifetime of variables β Not suitable for objects needing long-term storage
π Thatβs why heap is also used.
| Feature | Stack | Heap |
|---|---|---|
| Allocation | Automatic | Manual |
| Speed | Fast | Slower |
| Structure | LIFO | Random |
| Lifetime | Temporary | Long-term |
| Used for | Functions | Dynamic memory |
β Memory is divided into text, data, heap, stack β Stack stores function calls and local variables β Each function uses Activation Record β Stack follows LIFO order β Stack allocation is fast and automatic β Used heavily in recursive function calls
Storage organization refers to how memory is divided into segments like text, data, heap, and stack during program execution. Stack allocation of space is a runtime memory management technique where function calls are handled using a stack, and each call creates an activation record containing parameters, return address, local variables, and control information.
| Concept | Description |
|---|---|
| Storage organization | Memory layout of program |
| Stack | Stores function calls |
| Heap | Dynamic memory |
| Data segment | Global/static variables |
| Activation record | Function call memory block |
| Stack allocation | LIFO memory management |
Open this section to view and download past papers
Click on Show Past Papers to load available exam papers.