[Ilugc] C pgm - Recursive -segmentation fault

  • From: rsubr@xxxxxxxxxxxxxxxxxxxxxxxx (Raja Subramanian)
  • Date: Tue Sep 14 20:50:51 2004

Hi,

Sridhar R wrote:

AFAIK, when gcc is called with *no* command line options, *no*
optimization will be done.

While this is usually the case, it need not always be true.
GCC's default options are listed in the 'specs' file.

The OP did not mention the gcc flags used, and we can only speculate.

I just found out that gcc-2.95 does not know how to optimize tail calls.
However gcc-3.3.4 -foptimize-sibling-calls option completely eliminates
the recursive call.  Look at the C code and associated asm -

void foo(void) {
    static int i;
    i++;
    foo();
}


gcc -S test.s produces the following -

foo:
    pushl   %ebp
    movl    %esp, %ebp
    incl    i.0
    call    foo             <=== Function call!
    popl    %ebp
    ret


gcc -foptimize-sibling-calls -S test.c generates the following asm.

foo:
    pushl   %ebp
    movl    %esp, %ebp
.L3:
    incl    i.0
    jmp     .L3                 <=== Just a jump to L3

Note that this has eliminated tail recursion, and not just the sibling
call.  Good stuff.


- Raja

Other related posts: