Perhaps some sample code can illustrate how memory is managed using recursion. And how is the use of a static variable in a recursive subroutine a non-starter (one possible exception is the need for a global counter to count how many times a function is called, regardless of how the method is called (for example, from multi-threaded or sequential calls))
Given this C code:
#include <iostream> using namespace std; void callMe(int j) { static int i = 0; ++i; ++j; printf("Loop: %d\n", i); printf("i memory location: %p\n", &i); printf("j memory location: %p\n", &j); printf("\n"); // change i to j , // so the other next method invocations // or simultaneous(eg multi-threaded) method invocations // of this method can work independently from // other method invocations / simultaneous method invocations if ( i < 10 ) callMe(j); printf("Returning from loop %d\n", j); } int main() { callMe(0); printf("Next\n"); callMe(0); return 0; }
Output:
Loop: 1 i memory location: 0x804a038 j memory location: 0xbf8b69c0 Loop: 2 i memory location: 0x804a038 j memory location: 0xbf8b69b0 Loop: 3 i memory location: 0x804a038 j memory location: 0xbf8b69a0 Loop: 4 i memory location: 0x804a038 j memory location: 0xbf8b6990 Loop: 5 i memory location: 0x804a038 j memory location: 0xbf8b6980 Loop: 6 i memory location: 0x804a038 j memory location: 0xbf8b6970 Loop: 7 i memory location: 0x804a038 j memory location: 0xbf8b6960 Loop: 8 i memory location: 0x804a038 j memory location: 0xbf8b6950 Loop: 9 i memory location: 0x804a038 j memory location: 0xbf8b6940 Loop: 10 i memory location: 0x804a038 j memory location: 0xbf8b6930 Returning from loop 10 Returning from loop 9 Returning from loop 8 Returning from loop 7 Returning from loop 6 Returning from loop 5 Returning from loop 4 Returning from loop 3 Returning from loop 2 Returning from loop 1 Next Loop: 11 i memory location: 0x804a038 j memory location: 0xbf8b69c0 Returning from loop 1
As we can see, the static i variable has only one location in memory; and as such, values can survive between challenges. This is not advisable if you want to separate and overcome a given problem, especially with multi-threaded method calls. If two methods of simultaneous calling are performed using the callMe method, the other callMe may not fulfill its task, since these method calls simply use the same instance of the variable (static variables), these method calls will have a side effect for each other, these methods will not can work independently of each other since they do not have an independent copy of variables.
In the above code, even if it is not multithreaded, the next method call will not be able to complete its task, since the second call gets access to the same variable (static variable) and gets a value that has been corrupted by previous method calls
In simplified terms, static variables, even inside a function, are still a global variable. static variables inside a function simply prevent name collisions, but for all purposes and tasks static variables are global variables
To make the code run, change the value of if (i < 10) to if (j < 10)
By the way, non-static variables allocate their own memory allocated on the stack. If we do not have a stop condition, then after many recursive calls, the code above will lead to an error, this is where stackoverflow got its name. Suffice it to say that programmers love recursionツ
Live test: http://ideone.com/Xl86q