ACompressed recap
🧾
Static allocation fixes every address at compile time (no recursion, known sizes). Stack allocation adds recursion with LIFO lifetimes. Heap allocation adds data that outlives its creating call. Pointers that outlive their targets dangle; unreachable heap objects are garbage, reclaimed by garbage collection.
BMerged rule card
5.5 Static vs stack vs heap allocation: which language features force which
- Static: fixed addresses; needs no recursion, known sizes, no run-time data.
- Stack: supports recursion; lifetimes nested (LIFO).
- Heap: lifetimes beyond calls (malloc/new, objects, returned closures).
- Dangling reference: pointer outlives its target (address of a local, use after free).
- Garbage collection: reclaim unreachable heap objects (reference counting, mark-and-sweep).
CInterleaved GATE-style questions
Q1MCQ1 markFeatures
Fortran 77, with no recursion and fixed-size data, can use:
- (A) only static allocation
- (B) only heap allocation
- (C) stack allocation required
- (D) garbage collection required
Q2MCQ1 markStatic local
A C static local variable keeps its value between calls because it is:
- (A) on the stack
- (B) statically allocated
- (C) on the heap
- (D) a register
Q3MSQ2 marksDangling
Which create dangling references?
- (A) returning &x where x is a local
- (B) using memory after free(p)
- (C) returning a malloc’d pointer
- (D) returning a copy of a local int
Q4MCQ1 markHeap
Returning a closure that uses a local of its creating function requires the local to live:
- (A) on the stack
- (B) in the heap
- (C) in a register
- (D) in the code area
Q5MCQ1 markVariable-size
A local array whose size n is a parameter can be allocated:
- (A) only statically
- (B) on the stack when the call starts
- (C) only on the heap
- (D) never
Q6MCQ2 marksGarbage
In mark-and-sweep garbage collection, marking starts from:
- (A) every heap object
- (B) the roots: globals, stack and registers
- (C) the code area
- (D) the most recently freed object
Q7MSQ1 markFacts
Which are true?
- (A) Stack allocation supports recursion.
- (B) Static allocation supports recursion.
- (C) Heap allocation supports data outliving calls.
- (D) Garbage collection works on the heap.
Q8MCQ1 markLeak
Heap memory that is never freed and no longer reachable (without a collector) is:
- (A) a dangling reference
- (B) a memory leak (garbage)
- (C) static data
- (D) a stack overflow
Answer Vault
Try the questions first. Open when you are ready to check.
Q1 Answer: (A)1 mark
One activation per procedure, all sizes known.
- (B) is wrong: No dynamic data.
- (C) is wrong: No recursion.
- (D) is wrong: No heap.
Re-read: Lesson 5.5 — Static vs stack vs heap allocation: which language features force which
Q2 Answer: (B)1 mark
One copy for the whole run.
- (A) is wrong: Stack copies vanish at return.
- (C) is wrong: Not requested at run time.
- (D) is wrong: No.
Re-read: Lesson 5.5 — Static vs stack vs heap allocation: which language features force which
Q3 Answer: (A) (B)2 marks
Heap memory and copied values are fine.
Re-read: Lesson 5.5 — Static vs stack vs heap allocation: which language features force which
Q4 Answer: (B)1 mark
It must outlive the creating call.
- (A) is wrong: Popped at return.
- (C) is wrong: No.
- (D) is wrong: No.
Re-read: Lesson 5.5 — Static vs stack vs heap allocation: which language features force which
Q5 Answer: (B)1 mark
Its size is fixed for the duration of the call.
- (A) is wrong: Size unknown at compile time.
- (C) is wrong: The stack works.
- (D) is wrong: It can.
Re-read: Lesson 5.5 — Static vs stack vs heap allocation: which language features force which
Q6 Answer: (B)2 marks
Anything reachable from the roots is live data.
- (A) is wrong: Then nothing would be garbage.
- (C) is wrong: No.
- (D) is wrong: No.
Re-read: Lesson 5.5 — Static vs stack vs heap allocation: which language features force which
Q7 Answer: (A) (C) (D)1 mark
Static allocation cannot support recursion.
Re-read: Lesson 5.5 — Static vs stack vs heap allocation: which language features force which
Q8 Answer: (B)1 mark
Allocated but unreachable.
- (A) is wrong: Dangling is the reverse (reachable pointer, freed target).
- (C) is wrong: No.
- (D) is wrong: No.
Re-read: Lesson 5.5 — Static vs stack vs heap allocation: which language features force which