10

Why does the following program segfault?

int main() { main(); }

Even though it is a recursion that does not end and is therefore invalid by definition, I don't see why it segfaults (gcc 4.4.3 and clang 1.5 (trunk)).

Ether
  • 53,118
  • 13
  • 86
  • 159
user299831
  • 437
  • 1
  • 5
  • 15

6 Answers6

35

You get a stack overflow (!)

Community
  • 1
  • 1
Martin Wickman
  • 19,662
  • 12
  • 82
  • 106
26

Because every time it calls itself it allocates a little bit of stack space; eventually it runs out of stack space and segfaults. I'm a bit surprised it goes with a segfault, though; I would have expected (drum roll) stack overflow!

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • This machine has 4GB of RAM and it segfaults in less than a second. I don't think it runs out of RAM. Do you mean the stack can only be so small that it happens that fast? – user299831 Mar 23 '10 at 11:27
  • 10
    @user2999831 Stack is usually limited to something like 1 megabyte. – sharptooth Mar 23 '10 at 11:28
  • @user299831: it has nothing to do with how much RAM you have in your system. For each thread, there is max stack size (1 MB on Visual Studio, can be changed). If you exceed that size, you get a stack overflow. – Naveen Mar 23 '10 at 11:29
  • @user299831: Stack != RAM, stack space is pre-allocated at program start and usually somewhat limited (I'm not even sure you typically get even the 1MB sharptooth mentioned, but my C programming is quite dated at this point). Also, today's machines are *very* fast, your program is doing nothing other than the recursive `main` call, which it can do very quickly indeed as it involves little more than incrementing (decrementing?) a register and executing a jump. – T.J. Crowder Mar 23 '10 at 11:30
  • Having a stack size limited only by the amount of available memory would only serve to consume tons of memory before reaching a stack-overflow. – Joachim Sauer Mar 23 '10 at 11:31
  • ok. I got it. Thank you. My stack is 8KB currently... ulimit -s returns 8192. – user299831 Mar 23 '10 at 11:33
  • 1
    The value returned by `ulimit -s` is in kB, so 8192 means 8MB. – caf Mar 23 '10 at 13:35
  • Note that `ulimit -s` gives you the *maximum* stack space; that doesn't mean that's what your program is actually using. http://ss64.com/bash/ulimit.html – T.J. Crowder Mar 23 '10 at 13:52
10
int main() { main(); }

will cause a stack overflow.

But,

an optimized version (not debug mode) like this:

int main() {
   return main();
}

will transform the recursion in a tail-recursive call, aka an infinite loop!

Nick Dandoulakis
  • 42,588
  • 16
  • 104
  • 136
3

it is recurse without a base case, which causes a stack overflow

Mihir Mehta
  • 13,743
  • 3
  • 64
  • 88
2

It leads to stack overflow that is diagnosed as segfault on your system.

sharptooth
  • 167,383
  • 100
  • 513
  • 979
1

Each function call add entires in stack and this entries will get removed from stack when function exit. Here we have recursive function call which doesn't have exit condition. So its a infinite number of function call one after another and this function never get exit and there entires never removed from the stack and it will lead to Stack overflow.

Manish
  • 21
  • 3