The advantage of tail recursion is that we call the function (recursion) again in the last action of the code, so the stack variables doesn't need to be saved.
So here do this will act the same? and I can't be sure that the string will stay In the stack and not will be cleaned?
void foo(char* c){
c[0]='b';
printf("%s",c);
}
void foo2(){
char str[]="aaa";
foo(str);
}
int main(){
foo2();
}
or I can be sure that the string in foo2
will stay until the function ends (gets to the end scope).
I am asking if str
that is local variable of function foo2
will still be available, or if this local variable (str
) will be cleaned because no more actions to do in this function foo2
after calling foo(str)
.
I want to understand if could be that in foo
I will try to use a string that is not available anymore.
Output:
baa
By just this example I can see that str
didn't disappear until got to the end of foo2
scope, but I want to know if this will be like this every time, and if yes how this makes sense with the idea of tail recursion that also calls a function in the last action.
My problem was that I didn't understand enough about tail call optimizations, the compiler optimizes tail recursion and this doesn't have anything about the logic that the local variable is available until leaving the scope. ty for your help.