Which of the following statements are CORRECT?
1) Static allocation of all data areas by a compiler makes it impossible to implement recursion. 2) Automatic garbage collection is essential to implement recursion. 3) Dynamic allocation of activation records is essential to implement recursion. 4) Both heap and stack are essential to implement recursion.
(A) 1 and 2 only
(B) 2 and 3 only
(C) 3 and 4 only
(D) 1 and 3 only
Answer: (D)
Explanation: 1) Static allocation of all data areas by a compiler makes it impossible to implement recursion.
True, dynamic allocate of memory is required for function call stack as number of calls is not known advance for recursive functions.
2) Automatic garbage collection is essential to implement recursion.
False, Automatic garbage collection is not essential.
3) Dynamic allocation of activation records is essential to implement recursion.
True, as number of calls or number of activation records is not known advance for recursive functions.
4) Both heap and stack are essential to implement recursion.
Heap is not needed for function calls. It is generally used for dynamic memory allocation by user (or programmer).
See Memory Layout of C Programs for details.