--- In [email protected], "John Matthews" <[EMAIL PROTECTED]> wrote: > > --- In [email protected], "odbapsvm" <cupofjava1961@> wrote: > > > > Does anyone know what happens if you call > > main from main and end up with an infinite loop? > > int main(void) > { > return main(); > } > > Compiled using gcc 3.4.6 under linux (CentOS 4). When run: > > Segentation fault > > What an anti-climax! I thought it would be much more interesting than > that :-) > > I modified the program to keep a count of the number of times main was > called: > > int main(void) > { > static int n; > n++; > return main(); > } > > I ran it under gdb and examined the value of n when it crashed: > 334058. Of no practical use of course...
...and of equal practical use, if compiled using -O2 (optimisation level 2), it doesn't crash at all- just sits there soaking up CPU. Presumably the compiler spots that the recursion is an example of tail recursion and optimises it into iteration, which doesn't require stack storage. http://en.wikipedia.org/wiki/Tail_recursion
