Usually, if I have a non-terminating recursive function, it wouldn't run forever because it would cause a stackoverflow which finally leads to a segmentation fault.
As far as I know, tail recursive functions replace the current call stack frame with a frame of the same function with actualized values.
I've drawn the conclusion, that tail recursive functions can't have a stackoverflow and thus, they should run forever.
Non-Tail-Recursive:
int factor_rec (int n) {
if (n == 0)
return 1;
return n * factor_rec (n - 1);
}
Tail-Recursive:
int factor_rec_tr (int n, int akk) {
if (n == 0)
return akk;
return factor_rec_tr (n - 1, n * akk);
}
However, both of those functions lead to a segmentation fault, when they are called with a negative value. Why?