0

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?

Lavair
  • 888
  • 10
  • 21
  • If they're called with a negative value, the `n == 0` case will never be matched, so you'll recurse infinitely. – Barmar Feb 23 '19 at 01:24
  • C is not required to implement tail recursion optimization, it's an implementation extension. It might only do this if you have maximum optimization set, since it makes debugging difficult. – Barmar Feb 23 '19 at 01:25
  • 2
    Your tail-recursive version doesn't work correctly. The base case should `retuirn akk`, not `return 1`. – Barmar Feb 23 '19 at 01:26
  • So even if I implement a function in a tail recursive way, C does not take advantage of that? I highly doubt that. – Lavair Feb 23 '19 at 01:27
  • 2
    Doubt it all you want, there's nothing in the C specification that requires it. Very few languages do, the main exception is Scheme, and perhaps some functional languages like ML. – Barmar Feb 23 '19 at 01:28
  • See https://stackoverflow.com/questions/490324/how-do-i-check-if-gcc-is-performing-tail-recursion-optimization – Barmar Feb 23 '19 at 01:29
  • 1
    Did you compile with optimizations, as @Barmar suggested? – jxh Feb 23 '19 at 01:29
  • @jxh it really did help to compile with optimizations. But why do I have to tell the compiler explicitly to watch out for tail recursive functions and other stuff. There is apparently no need to code efficient because I need to turn on optimizations anyway – Lavair Feb 23 '19 at 01:38
  • 1
    Most languages don't automatically recognize tail recursion and convert a stack of function calls to iteration. However your code appears to have other issues. – seand Feb 23 '19 at 01:47
  • @Lavair There are many reasons. A programmer will often want to inspect local variables of the code, but optimizations may remove them. Optimizations may reorder code in ways that make it difficult to understand how the machine instructions correlate with the source.Optimizations generally make debugging logical errors more difficult. – jxh Feb 23 '19 at 05:12
  • 1
    A compiler will generally not change your algorithm from quadratic to linear. It is still important to write efficient code. But optimizations allow a programmer to focus on making the code readable and understandable, which aids in making sure the code functions correctly first. – jxh Feb 23 '19 at 05:17

0 Answers0