This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Hi, my first post here.
I've been trying to wrap my mind around memory allocation for recursive functions.
As far as my understanding goes a recursive function has to go all the way down in the recursion chain until a definite value is returned and consecutive results bubble up. Every recursion step towards that definite return value requires some memory allocation which can lead to stack overflow (like in the example below).
In a book, i came across an example which supposedly solves the stack overflow problem but i cant see how. Is memory still allocated in same fashion as in example above?
I quickly looked at the algorithm, and it appears to do exactly the same thing as the first one, the only difference being that the first one calculates a factorial starting with n, and the second one starts with 1.
As a matter of fact, the second algorithm will fail for n == 0.
They both use O(n) stack frames.
The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.