views:

505

answers:

7

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)).

+17  A: 

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
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?
@user2999831 Stack is usually limited to something like 1 megabyte.
sharptooth
@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
@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
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
ok. I got it. Thank you. My stack is 8KB currently... ulimit -s returns 8192.
The value returned by `ulimit -s` is in kB, so 8192 means 8MB.
caf
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
+20  A: 

You get a stack overflow (!)

Martin Wickman
+1: Clever link!
Jonathan Leffler
!!! Brilliant link.
T.J. Crowder
It is amazing, that when I click that link the very first time, I immediately get to stack overflow! Not a very deep stack, I guess...
AndreyT
+2  A: 

Welcome to the world of stackoverflow :-)

Naveen
+1  A: 

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

sharptooth
+2  A: 

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

mihirpmehta
+8  A: 
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 D
Actually for this example, gcc -O3 will optimise the loop away too.
Lachlan Roche
@Nick How the two are different?
Adil
@Adil, it's compiler dependent, but it's possible that if we don't explicitly "return" the main the compiler may not convert it to a tail recursion. (example cases: `if (1) {main();} return 0;` and `if (1) {return main();} return 0;`)
Nick D
A: 

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