ACompressed recap
🧾
A running program’s memory has a code area, a static area (globals, static locals, literals; sizes known at compile time), a stack of records for active calls, and a heap for explicitly requested memory with unpredictable lifetimes. Heap and stack usually grow toward each other through the free memory.
BMerged rule card
5.1 Storage organization: code, static, stack, heap
- 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.
CInterleaved GATE-style questions
Q1MCQ1 markAreas
A static int counter; declared inside a C function lives in:
- (A) the stack
- (B) the heap
- (C) the static data area
- (D) a register only
Q2MCQ1 markAreas
The machine instructions of a program are kept in:
- (A) the heap
- (B) the code area
- (C) the stack
- (D) the symbol table
Q3MSQ1 markRun time
Which are managed at run time rather than fixed by the compiler?
- (A) the number of stack records in use
- (B) heap blocks
- (C) the offset of a local inside its record
- (D) the address of a global (after linking)
Q4NAT1 markCopies
A recursive function has 2 ordinary locals and 1 static local, and is active 5 times at once. How many local variable copies exist in total (stack + static)?
Numerical answer: ________
Q5MCQ2 marksLifetime
A function allocates a buffer with malloc and returns its address. After the return, the buffer:
- (A) is freed automatically
- (B) remains valid until freed
- (C) moves to the stack
- (D) moves to the static area
Q6MCQ1 markGrowth
Running out of memory in the usual layout happens when:
- (A) the code area is full
- (B) the heap and the stack reach each other
- (C) the static area is empty
- (D) a function returns
Q7MSQ2 marksLiterals
For char *s = "hi"; as a local variable, which are true?
- (A) s lives on the stack
- (B) "hi" lives in the static area
- (C) "hi" lives on the stack
- (D) s lives in the heap
Q8MCQ1 markStatic
Why can global variables be given fixed addresses before the program runs?
- (A) their sizes and number are known at compile time
- (B) they are never used
- (C) they live in the heap
- (D) they are recursive
Answer Vault
Try the questions first. Open when you are ready to check.
Q1 Answer: (C)1 mark
One copy for the whole run, keeping its value between calls.
- (A) is wrong: Not per call.
- (B) is wrong: Not requested at run time.
- (D) is wrong: It needs a permanent address.
Re-read: Lesson 5.1 — Storage organization: code, static, stack, heap
Q2 Answer: (B)1 mark
Also called the text segment.
- (A) is wrong: Heap holds requested data.
- (C) is wrong: The stack holds call records.
- (D) is wrong: The symbol table is a compile-time structure.
Re-read: Lesson 5.1 — Storage organization: code, static, stack, heap
Q3 Answer: (A) (B)1 mark
Offsets inside a record and global addresses are fixed before the run.
Re-read: Lesson 5.1 — Storage organization: code, static, stack, heap
Q4 Answer: 111 mark
2 × 5 on the stack + 1 static = 11.
Re-read: Lesson 5.1 — Storage organization: code, static, stack, heap
Q5 Answer: (B)2 marks
Heap memory is independent of call lifetimes.
- (A) is wrong: Only the function’s stack record is freed.
- (C) is wrong: Memory does not move.
- (D) is wrong: No.
Re-read: Lesson 5.1 — Storage organization: code, static, stack, heap
Q6 Answer: (B)1 mark
They share the free region.
- (A) is wrong: Code size is fixed.
- (C) is wrong: No.
- (D) is wrong: Returning frees stack space.
Re-read: Lesson 5.1 — Storage organization: code, static, stack, heap
Q7 Answer: (A) (B)2 marks
The pointer is local; the literal is static data.
Re-read: Lesson 5.1 — Storage organization: code, static, stack, heap
Q8 Answer: (A)1 mark
Exactly one copy each, known sizes.
- (B) is wrong: They are used.
- (C) is wrong: They are static data.
- (D) is wrong: Variables are not recursive.
Re-read: Lesson 5.1 — Storage organization: code, static, stack, heap