C run time stackoverflow
c, stack-overflow
Solution
It's frame pointers and return addresses, usually. See e.g. wikipedia's "call stack" article. In case you're curious:
$ gcc -S test.c # <--- assembles, but does not compile, test.c; output in test.s
$ cat test.s
// [some contents snipped]
_doHello:
pushl %ebp // <--- pushes address of stack frame onto stack
movl %esp, %ebp // <--- sets up new stack frame
call _doHello // <--- pushes return value onto stack, makes call
popl %ebp // <--- pops address of stack frame off stack
ret // <--- pops return value off stack, returns to it
For fun, try "-fomit-frame-pointers":
$ gcc -fomit-frame-pointers -S test.c
$ cat test.s
// [some contents snipped]
_doHello:
call _doHello // <--- pushes return value onto stack, makes call
ret // <--- pops return value off stack, returns to it
For even more fun, let's see what happens when we turn on optimizations:
$ gcc -fomit-frame-pointers -O4 -S test.c # <--- heavy optimization
$ cat test.s
// [some contents snipped]
_doHello:
L2:
jmp L2 // <--- no more stack operations!
The last version will run forever, rather than quitting, at least on my setup (cygwin, at the moment).
To diagnose issues like this, you can run in your favorite debugger (e.g. Microsoft Visual C++ or gdb) or examine stackdumps that are usually generated by most systems (.core or .stackdump files) using those debuggers.
You may also be able to set a hardware breakpoint near the top of your stack, if your debugger supports this -- any attempt to write to this variable, and your stack may be getting full. Some operating systems have additional mechanisms to alert you to stack overflows.
Finally, debugging environments such as valgrind or Application Verifier may help.
Problem
``` #include <stdio.h> int doHello(){ doHello(); } int main(){ doHello(); printf("\nLeaving Main"); return 0; } ``` When you run this the program quits without printing the message "Leaving Main" on the screen. This is a case of Stack Overflow and because of which program is terminating but I don't see any error messages on the command window. (Ran on Windows/Cygwin/) Q1. I have not declared any local variables in the doHello function but still stack is getting used. Is this is because of - return values - information stored about the function calls? Clarification Q2. How to debug such cases in your program? I am not asking to debug an infinite loop which I mentioned above. for example: ``` #define SIZE 512*1024 void doOVerflow(){ char str[SIZE]; doHello(); } void doHello(){ char strHello[256]; // stack gets filled up at this point doNothing(); // program terminates and function doNothing does not get called } ``` EDIT: Q3. What information is stored in run time stack?