Heap allocation is required for languages that :
2017
Heap allocation is required for languages that :
- A.
use dynamic scope rules
- B.
support dynamic data structures
- C.
support recursion
- D.
support recursion and dynamic data structures
Attempted by 558 students.
Show answer & explanation
Correct answer: B
Answer: support dynamic data structures
Explanation:
Key idea: Heap allocation is used when data must outlive the function call that created it.
Dynamic data structures (for example, linked lists, trees, or objects allocated at runtime) often need memory with a lifetime independent of any single activation record, so they are allocated on the heap.
Recursion itself is handled via the call stack (activation records). Recursion does not inherently require heap allocation unless the program also creates persistent dynamic data.
Dynamic scope is about how names are resolved at runtime and is unrelated to whether memory must be taken from the heap.
Example: If a function creates a linked-list node that must remain valid after the function returns, that node must be allocated on the heap; stack allocation would be reclaimed when the function ends.