[Ilugc] C pgm - Recursive -segmentation fault

  • From: Kannan_Ranganathan@xxxxxxxxxx (Kannan_Ranganathan)
  • Date: Tue Sep 14 21:08:28 2004


Wow ..This is something new. infact the whole argument is centered on
weather a recursive program will run forever or not.

Initially I had a belief that a recursive program will run forever. After
all these discussions I reconciled recursion is dependent on stack memory.

There were strong objections when I mentioned it was running well past 12
hrs. Whatever you have mentioned is quite contrary from rest of the post.

Can you pls elaborate. 



-----Original Message-----
From: ilugc-bounces@xxxxxxxxxxxxx
To: ilugc@xxxxxxxxxxxxxxxxxx
Sent: 9/14/2004 8:50 PM
Subject: Re: [Ilugc] C pgm - Recursive -segmentation fault

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

_______________________________________________
To unsubscribe, email ilugc-request@xxxxxxxxxxxxx with 
"unsubscribe <password> address"
in the subject or body of the message.  
http://www.ae.iitm.ac.in/mailman/listinfo/ilugc
************************************************************************** 
This email (including any attachments) is intended for the sole use of the
intended recipient/s and may contain material that is CONFIDENTIAL AND
PRIVATE COMPANY INFORMATION. Any review or reliance by others or copying or
distribution or forwarding of any or all of the contents in this message is
STRICTLY PROHIBITED. If you are not the intended recipient, please contact
the sender by email and delete all copies; your cooperation in this regard
is appreciated.
**************************************************************************

Other related posts: