-6

Compiled this on Windows using GCC. It crashed immediately with Exception Code: c00000fd.

Edit : Tried compiling following code (for visible output) and it causes stackoverflow.

#include<stdio.h>
int main(void)
{
printf("Hello World\n");
return main();
}

Output -

>gcc trailORoverflow.c -o trailORoverflow.exe

Hello World
Hello World
Hello World
Hello World
Hello World
Hello World

It keeps printing Hello World for sometime and crashes.

Edit: Didn't crash with O2, O3 and -O1 -foptimize-sibling-calls optimizations.

Mark Evans
  • 974
  • 1
  • 11
  • 29
  • Perhaps the correct answer has something to do with tail call recursion. iirc GCC will implement tail call recursion, i.e. optimizing away recursive calls, when possible – dreadiscool May 18 '16 at 18:36
  • 1
    Because the C standard does not require a specific optimisation. (It does not even require using a stack for local variables). And use a correct prototype-style function declarator: `main(void)`. – too honest for this site May 18 '16 at 18:37
  • it definitely makes sense to try gcc with different optimization flags and probably some combination will give you infinite loop you want – Iłya Bursov May 18 '16 at 18:39
  • gcc or g++ ? Then there can be a difference. – phoxis May 18 '16 at 18:49
  • 5
    Use `-O2`, `-O3`, or `-O1 -foptimize-sibling-calls` – Mark Plotnick May 18 '16 at 18:58
  • @MarkPlotnick It worked! I tried with all optimizations you suggested and it didn't cause stackoverflow. Why did it cause stackoverflow the first time? – Mark Evans May 18 '16 at 19:30
  • 1
    gcc doesn't perform tail recursion optimization by default, possibly because it makes programs harder to debug and unwind after exceptions. So your unoptimized program used up a bit of stack space every time it did a call (`callq main` followed by `push %rbp`), until it reached the limit imposed by the OS. An optimized program would do something like `jmp main`. I don't have gcc on Windows so can't provide a comprehensive answer, but someone else will do so. – Mark Plotnick May 18 '16 at 19:41

1 Answers1

2

The code you have shown will call main infinitely, and therefore will result a stack overflow. This is true in the case of any function and not specific to main. Each function call a stack frame is created in memory, and as infinite such frames is created as the recursion goes deep, you get a stackoverflow.

But if you make a proper base termination like the example as follows, for recursive calls in main then there is an interesting thing.

int main (void)
{
  static int x = 100;

  if (x == 0)
  {
    return 0;
  }
  x--;
  printf ("%d\n", x);
  main ();
  return 0;
}

There is a difference in calling main recursively in C and C++ Language, and I think it is interesting to point that out. Here is a post I wrote, I am giving some explanation about it.

C++ Standards talk about these in

Section 3.6.1 Paragraph 3

Recursive calls are permitted, except to the function named main. 

and Section 5.2.2 Paragraph 9

The function main shall not be used within the program. … … … 

I did not find any such restriction in the C standards. What I found about recursive calls in the C99 standards in Section 6.5.2.2 Paragraph 11 is as follows

Recursive function calls shall be permitted, both directly and indirectly through any chain of other functions. 

Therefore calling main recursively in C Language is deterministic. But as per C++ standards calling main from any function or recursively is not allowed.

phoxis
  • 60,131
  • 14
  • 81
  • 117