[Ilugc] C pgm - Recursive -segmentation fault

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

Hi,

Kannan_Ranganathan wrote:

void R()
{
    static int i =0;
    i++;
  /*  printf("%d\n",i);*/

    R();
}

int main()
{
    R();
}

in my Home ...the same program which was sent earlier.

This program is still running well above 12 hrs (running since last night
till I left for office this morning  ) ... My machine is a P2 /64 MB ram. 

Compiler optimisation is at work here.

Lookup "tail recursion elimination" in GCC docs or your favourite
compilers book.

IIRC, GCC can also to "tail call elimination".  Both of which can
eliminate the recursive function call and effectively turn it into a
loop.

Try compiling without optimisation using the -O0 gcc flag and observe the
results.  To see what exactly happens when you optimise, try gcc -S,
which will provide you the asm code.

- Raja

Other related posts: