The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+24 votes
2.6k views

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. 
  1. $1$ and $2$ only
  2. $2$ and $3$ only
  3. $3$ and $4$ only
  4. $1$ and $3$ only
in Compiler Design by Veteran (100k points)
edited by | 2.6k views
0
None of the answer given below are clear.

Even the best answer is using answer elimination.
0
Recursion can not be implemented without stack. Heap is not needed in recursion if there is no use of dynamic memory allocation like pointers.

2 Answers

+25 votes
Best answer

It will be D.

option 2 is wrong because it is not necessary to have automatic garbage collection to implement recursion.

option 4 is wrong because it says that both are required to implement recursion, which is wrong. Either of them will suffice.

by Boss (19.9k points)
edited by
0
Can we implement recursion with just heap and not stack?
0
yes. Allocation and deallocation can be done at any time in Heap. It also supports recursion.
+41
Actually it is not possible. Recursion needs a stack structure. It can be done using a heap, but then we have to simulate s stack using the heap.

http://stackoverflow.com/questions/2807131/recursion-using-only-heap-area
0
Sir , what is the meaning of dynamic allocation of activation record , since dynamic allocation occurs in the heap while we have activation record in the stack .
0
Why d is incorrect? We need stack for storing functions local variables, and if each function uses dynamically allocated variables (suppose malloc() is used) then we also need heap area. So, aren't they both essential? Moreover, for option A, don't we need dynamic allocation scheme for data areas as we don't know beforehand how many function calls are going to be there?
0
@Arjun sir:
simulating a stack using heap means: we shouldn't consider this case?
+4
in lisp activation record are allocated in Heap . So then 1 and 3 can be correct .
0
yes, stack structure is required for recursion bcoz, in a heap, allocation & de-allocation is done randomly.
0
Dynamic allocation means allocating the required portion of memory during run-time.

In the stack the memory for storing activation records is allocated at run-time according to actual function calling sequence.

Hence allocating stack space for Activation Records is also dynamic allocation, as it is done during run time.
0

@Arjun Sir

Heap is having structure as CBT ..but for Recursion we need LIFO structure ...which only stack can provide ..

Is this right sir ??

+14 votes

1) In Static allocation of memory , we cannot implement recursion. As in recursion we cannot determine at first how many time function will be called

2)Automatic garbage collection done in JAVA (JVM done this) and LISP. But recursion we can implement in C too.So, Automatic Garbage Collection is not essential for Recursion

3) True. Same reason as 1)

4)Stack implement recursion, but heap is not necessary for recursion

by Veteran (115k points)
0
@arjun sir if one of the option is given as 1,3,4 then we click on this option ?? in exam plz reply
0
@rajan statement 4 is false because either of them (heap or stack) is sufficient to implement recursion.
0
@akash.dinkar12 Option 1 says static allocation ie stack is imp , Option 2 says heap is also important ie dynamic allocation is also imp .So both of them are needed making Option 4 right. Am i right?
0

Statement 1st is easy and we got it directly from nptel by sir YN srikant Lectures on run time environment.

statement 2nd is also well understood as we know c not allowed automatic GC and we can implement recursion there.

statement 4th is also fine there is quite difficulties to implementation recursion in heap because we know heap allow random allocation and de-allocation and moreover there is a simulation required for a well formed data structure.

Only Statement 3rd i am not getting here can anybody explain this? 

Acc to my point of view activation record is created whenever there will be a entry of a procedure thats why there is a need of recursion.

Correct me...

 

Answer:

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,095 questions
55,333 answers
190,852 comments
86,258 users