2

Does Stack overflow gives segmentation fault in this case, when this infinite recursion runs. I was expecting an error message like "Stack Overflow"!

#include <stdio.h>
int main(){
static int a=1;
printf("%d\n",a);
a++;
main();
return 0;
}
4
  • Probably depends on the platform. Commented Aug 18, 2011 at 20:48
  • Stackoverflow does happen here, but you get this error message when debugging your program, not when running it. Commented Aug 18, 2011 at 20:50
  • @Mr.TAMER: Can you brief me how to debug and see the stack overflow message. Commented Aug 18, 2011 at 21:03
  • @user302520: Every C++ compiler can run any C code, so I use MS Visual C++ for debugging C codes. It's just a normal debugging (F5 for example). Commented Aug 18, 2011 at 21:08

4 Answers 4

4

It will cause a segmentation fault because the stack will overflow.

What happens is that each call to main pushes some more data onto the stack so that your program knows where to jump once it returns from main(). Eventually, you will run out of stack space (a stack overflow). At this point, your next call to main will try to push data to the stack. Since there is no more stack space available, it will accidentally write to an invalid memory location thus triggering the segmentation fault. This is similar to when you write past the end of an array.

Sign up to request clarification or add additional context in comments.

Comments

2

A stack overflow is a cause; the effect of the stack overflow is (in many cases) a segmentation fault. It depends on the OS, architecture and runtime environment whether the code the creates the error message will be able to deduce from the effect that the cause was a stack overflow. But most don't bother to try.

Comments

0

As others before posted, it depends on the environment (os, hardware, compiler, etc.). Depending on this, the error message might not reflect the actual cause (as often when doing invalid memory access). That said, running it in something like valgrind should always give you reliable information on the cause of such access.

Comments

-2

You're only doing a int overflow.

After reaching his maximum value it go on his minus value.

This recursion will smash you call stack!

void lol()
{
lol();
}

Here it is for buffer overflow: https://www.owasp.org/index.php/Buffer_Overflow

It's not a programming fault but some security issue.

Like you don't verify the password input len, and the guy send you 4000000 chars that will overflow your char* if len declared like char[6].

6 Comments

There's a clear infinite recursion in the code example in the question. main() calls main() in a non-tail position which will inevitably fill up the stack.
If int is 64bit on that platform I heavily doubt it will overflow before the stack...
@inflagranti, it depends on the size of the stack and whether it can grow or not. Some platforms allow you to fix the size of the stack.
@MSN: That must be some gigantic stack, though. Like 2^63 frames worth of stack - I'd like to see (or rather own) the machine where you can fill a stack of that size...
@inflagranti, ah I totally misread your comment. You meant integer overflow before stack overflow.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.