I'm trying to call the same function, before the first call to it is completed. My program crashes. Any ideas? And is it possible to make each "recall/callback" unique so they dont affect each others variables inside the function? Maybe thats not the issue here but something is failing, and I dont know what.
Here is a function calling itself before the first call to it is completed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <iostream>
usingnamespace std;
void function(int value)
{
if (value == 1) {function(0);}
cout << value << '\n';
return;
}
int main()
{
function(1);
}
If your program crashes, it's nothing to do with recursion. It'l be because you're simply doing something dangerous.
And is it possible to make each "recall/callback" unique so they dont affect each others variables inside the function?
Every call to a function is unique. If the functions don't contain any shared values such as statics or globals or some such, then they can't interfere with each other.
Because your function calls extend_path an infinite number of times, but you don't have infinite memory on your stack so when all the stack is full with a huge number of calls to the function, it crashes.
Calling a function uses some stack space. When the function exits, that stack space is freed. Your code calls extend_path over and over and over, using more and more and more stack, until it's all gone. None of your calls to extend_path will ever finish.