A program's main calls f(5), where int f(int n) { if (n <= 1) return 1; return f(n-1) + f(n-2); }. Left operands are evaluated first. What is the maximum number of activation records on the run-time stack at any instant, counting the record of main?
Answer and solution
Answer:
The deepest point follows the leftmost chain: f(5), f(4), f(3), f(2), f(1).
That is 5 activations of f; no other chain is longer, since each call goes at most one level deeper than its caller by calling f(n-1).
Add main's record: 5 + 1 = 6.