Which languages necessarily need heap allocation in the runtime environment?
2010
Which languages necessarily need heap allocation in the runtime environment?
- A.
Those that support recursion
- B.
Those that use dynamic scoping
- C.
Those that allow dynamic data structures
- D.
Those that use global variables
Attempted by 809 students.
Show answer & explanation
Correct answer: C
Key idea: heap allocation is required when values need flexible size or must live beyond the creating function's activation.
When to use the heap: dynamic data structures such as linked lists, trees, resizable arrays, and objects whose size or lifetime is not known at compile time. These need memory that can persist independently of a single stack frame.
Memory management: the heap is managed either manually (free/delete) or automatically (garbage collection), depending on the language.
What does not necessarily require the heap: recursion (activation records are normally on the stack), dynamic scoping (can be implemented with stack frames or a dynamic chain), and global variables (stored in static memory). Specific language implementations might still choose to use the heap for some of these features, but they do not inherently mandate heap allocation.