Spring 2002, CSE 428: Solution of Assignment 3


Note: In this solution, the stack is drawn as growing bottom-up. If you have adopted the opposite convention, it's not a problem.


Exercise 1

At the moment in which the string "returning the result of fibo(1)" is printed there are four activation records for fibo present on the stack (there had been five activated in total, but the call of fibo(2) has generated no recursive calls and its activation record has been removed before the allocation for the call of fibo(1) (called by fibo(3)).

The following drawing illustrates the situation.


Exercise 2

  1. Since l goes out of scope the object to which it was pointing (namely the object of class list containing only the field first), and the three objects of class element (the whole list) that are pointed by first, are left as memory leaks. No dangling pointers.

  2. Since the l of p() is returned and its value is held by the l of q(), there are no memory leaks. There are no dangling pointers either.

  3. Since there is no destructor method in list, when l is deleted only the object of class list (containing the field first) is returned to the free list, the elements of the list become memory leaks. Furthermore, l becomes a dangling pointer.

  4. This case is similar to the previous one, but now l is local to p(), hence it goes out of scope when p() returns, and thus it is not a dangling pointer.

  5. This case is similar to the previous one, but now the destructor method of the classes list and element ensures that the elements of the list are deleted as well. Thus there is no memory leak. And, like in previous case, there are no dangling pointers either.


Exercise 3

  1. The following drawing illustrates the situation on the stack at the moment in which the second value is printed, in the case of static scope.