Recursive Functions (58)

36 Name: #!/usr/bin/anonymous : 2008-09-23 14:44 ID:JP38qf3E

>>35

I understand you think you know more about C and how C compilers are implemented than you actually do. I'll explain the one you had the most trouble with, but this is extremely time consuming and I won't want to waste my time if you're just going to fag it up with more hostility.

extern int baz(int *z);
static void foo(int x) {
if (baz(&x) <= 0) return;
foo(x-1);
}
void bar(int x) {
foo(&x);
}

GCC produces:

It should look something like this:

This is tail recursive, and the reason gcc fails to eliminate the recursive call because of the &. Remove it and you'll see GCC can eliminate it just fine (even though the resulting logic is wrong).

There's no reason gcc can't optimize alloca() away for similar reasons, or if the tail-call involves two (mutually cooperating) functions when the number of arguments changes.

You should not write code assuming gcc can optimize it, if you don't know it will. Disassemble it's output, and verify it did. Comment that your logic expects this behavior.

This thread has been closed. You cannot post in this thread any longer.