0 · Recall Warm-up
Answer from memory first. Answers are in the Answer Vault at the end of this lesson.
- Name the three leader rules. from Lesson 4.4
- In jumping code for B₁ && B₂, where does B₁’s false exit go? from Lesson 4.2.3
- What does the loader do? from Lesson 0.1.2
1 · The Story
📖
Where things are kept during a performance
The booklet is printed; now Mr. Patel’s evening actually runs. Rhea in the Records Room manages everything kept during the performance, and her room has four zones.
The script sits on a locked shelf: everyone reads it, nobody changes it. The notice board holds facts that exist for the whole evening: the hall name, the ticket price. Its size is known before the doors open.
Each time someone starts speaking, Rhea puts a tray on a pile for that speaker’s notes; when they finish, the tray goes away. Speakers who interrupt others put their trays on top.
And there is a lost-and-found corner for things that arrive at unpredictable times and are kept for unpredictable lengths: a guest’s umbrella may stay long after its owner left the stage. Rhea fills the tray pile from the ceiling down and the lost-and-found from the floor up, so the free space in the middle serves both.
2 · Predict First
🤔
A C function calls malloc(100), stores the pointer in a local variable p, and returns p. After the function returns, where do the 100 bytes still live?
Write your guess down. The answer is in the vault.
3 · Core Concept
The run-time memory of a program
When a program runs, the operating system gives it a region of memory that the compiler has planned in four areas:
- Code (text): the machine instructions. Its size is known at compile time; it is usually read-only.
- Static data: global variables, static local variables, constants and string literals, whose sizes are known at compile time and which exist for the whole run. Their addresses can be fixed by the compiler and linker.
- Stack: one record per currently active function call, holding its local variables, parameters and bookkeeping. It grows and shrinks as functions are called and return.
- Heap: memory requested explicitly at run time (malloc, new) whose lifetime does not follow the call/return order. It is released explicitly (free) or by an automatic collector.
Layout and growth
A common layout puts code at low addresses, then static data, then the heap growing upward, with the stack starting at high addresses and growing downward. The free memory between them can be used by whichever needs it; the program runs out of memory when they reach each other. (Directions differ between systems; the idea of two areas growing toward each other does not.)
Compile time vs run time decisions
The compiler knows the size of the code and of all static data, so it assigns them fixed (relative) addresses. It does not know how many calls will be active at once (recursion) or how much heap will be requested, so those are managed at run time. Within one function’s record, though, the compiler knows the offset of every local variable.
Where C objects live
| Object in C | Area |
|---|---|
int count; (global) | static data |
static int calls; inside a function | static data |
"hello" (string literal) | static data (often a read-only part) |
int i; (local) | stack |
| function parameters | stack (or registers) |
malloc(100) / new Node() | heap |
| the instructions of main | code |
Story → compiler term
| In the story | In the compiler |
|---|---|
| the locked script shelf | code (text) area |
| the notice board for the whole evening | static data area |
| the tray for each current speaker | stack (one record per active call) |
| lost-and-found with unpredictable lifetimes | heap |
| pile from the ceiling down, lost-and-found from the floor up | stack and heap grow toward each other |
| size of the notice board known before doors open | static sizes known at compile time |
4 · Visual
Typical run-time memory layout, low addresses at the bottom.
5 · Worked Examples
Example 1 Easy
Place each object of this program in its area: a global int total;, a local int i; in main, static int n; inside f, the literal "done", a block returned by malloc, and the instructions of f.
Solution
total, n and "done": static data. i: stack (main’s record). The malloc block: heap. f’s instructions: code. Like Rhea’s zones: notice board, tray, lost-and-found, script.
Example 2 Medium
Why do the stack and heap usually grow toward each other from opposite ends?
Solution
Neither size is known in advance. Placing them at opposite ends of the free region lets either use all of the free space; memory is exhausted only when they reach each other. Two fixed-size areas would waste space in one while the other overflows.
Example 3 GATE
A recursive function f has one local variable. f is called with recursion depth 50. How many copies of that local exist at the deepest point, and in which area?
Solution
50 copies, on the stack, one in each active call’s record. A static local would have just one copy in the static area, shared by all calls.
6 · Shortcuts
⚡
Lifetime decides the area: whole run → static; one call → stack; independent of calls → heap; instructions → code. Valid when: any “where is this stored?” question.
⚡
Size known at compile time and one copy? Static is possible. Many copies at once (recursion) → stack. Valid when: deciding whether static storage can work.
7 · GATE Traps
⚠️
- A
staticlocal variable lives in the static area, not on the stack, and keeps its value between calls. - A pointer variable can live on the stack while what it points to lives in the heap.
- String literals are static data, not stack data.
- Growth directions (heap up, stack down) are a convention; the exam asks for the usual one.
- The compiler fixes offsets inside a record, not the record’s run-time address.
8 · Memory Hook
🧠
Script, notice board, tray pile, lost-and-found = code, static, stack, heap.
9 · Connect the Dots
The stack holds one record per active call. The next lesson opens one of those trays: what a record contains, who fills in which part at a call, and how recursion piles up records.
10 · Self-Test
GATE format. Try every question before opening the vault.
Q1MCQ1 markAreas
Memory obtained with malloc is allocated in the:
- (A) code area
- (B) static area
- (C) stack
- (D) heap
Q2MSQ1 markStatic area
Which normally live in the static data area?
- (A) global variables
- (B) static local variables
- (C) local variables of a recursive function
- (D) string literals
Q3MCQ1 markGrowth
In the usual layout, the stack and heap:
- (A) both grow upward
- (B) grow toward each other
- (C) never change size
- (D) are the same area
Q4NAT1 markCopies
A function with one ordinary local variable is active 12 times at once (recursion). How many copies of the local exist on the stack?
Numerical answer: ________
Q5MCQ1 markCompile time
Which size is known exactly at compile time?
- (A) the total heap used
- (B) the maximum stack depth of a recursive program
- (C) the size of the code and static data
- (D) the number of calls to a function
11 · Rule Card
- Areas: code (read-only), static data (globals, static locals, constants, literals), stack (call records), heap (malloc/new).
- Usual layout: code, static at low addresses; heap grows up; stack grows down; free memory between.
- Static sizes fixed at compile time; stack and heap managed at run time.
- Lifetime: whole run → static; one call → stack; arbitrary → heap.
12 · Concept Ledger
code (text) areastatic data arearun-time stackheapfree memorystatic local variable
13 · Answer Vault
Answer Vault
Try the questions first. Open when you are ready to check.
Warm-up
- The first instruction; every jump target; every instruction right after a jump.
- To B.false: one false operand makes the whole && false.
- It copies the executable into memory, adjusts addresses if needed and starts the program.
Predict First
In the heap. The local variable p (on the stack) disappears when the function returns, but heap memory lives until it is freed.
Self-Test
Q1 Answer: (D)1 mark
Explicit run-time requests come from the heap.
- (A) is wrong: Code holds instructions.
- (B) is wrong: Static data is fixed at compile time.
- (C) is wrong: The stack holds call records.
Re-read: Lesson 5.1 — Storage organization: code, static, stack, heap
Q2 Answer: (A) (B) (D)1 mark
Ordinary locals live in the calling record on the stack.
Re-read: Lesson 5.1 — Storage organization: code, static, stack, heap
Q3 Answer: (B)1 mark
Heap upward, stack downward, sharing the free memory.
- (A) is wrong: They grow in opposite directions.
- (C) is wrong: Both change during the run.
- (D) is wrong: Different areas.
Re-read: Lesson 5.1 — Storage organization: code, static, stack, heap
Q4 Answer: 121 mark
One per active call: 12.
Re-read: Lesson 5.1 — Storage organization: code, static, stack, heap
Q5 Answer: (C)1 mark
The compiler produces the code and lays out static data.
- (A) is wrong: Depends on run-time requests.
- (B) is wrong: Depends on input.
- (D) is wrong: Depends on input.
Re-read: Lesson 5.1 — Storage organization: code, static, stack, heap