Callback Function Stack Overflow

J

jack113256

Hi everyone:
I have a question in using Callback function, there is my code:


/******* code start *********/
#include <stdio.h>

void a();
void b();
void run();
int state;

int main()
{
state = 0;
run();
}

void run()
{
if(state){
a();
}else{
b();
}
}
void a()
{
printf("a");
state = 0;
run();
}
void b()
{
printf("x");
state = 1;
run();
}

/******* code end *********/

In my opinion, the program will print "axaxaxa..." and so on, and
because the function a() and b() is called in the context of function
run(), the call trace will take more and more memory for the next
function call, which is like a recursive function call, and will
finailly stoped due to a function stack overflow.
the result is : the screen does print out "axaxaxax......" but the
function stack overflow does not happen.
Why???

I use x86-winXP machine. the windows taskmanager shows that the
process's memory does not increase. Why???
can anyone tell me the reason?
 
S

Stephen Montgomery-Smith

jack113256 said:
Hi everyone:
I have a question in using Callback function, there is my code:


/******* code start *********/
#include <stdio.h>

void a();
void b();
void run();
int state;

int main()
{
state = 0;
run();
}

void run()
{
if(state){
a();
}else{
b();
}
}
void a()
{
printf("a");
state = 0;
run();
}
void b()
{
printf("x");
state = 1;
run();
}

/******* code end *********/

In my opinion, the program will print "axaxaxa..." and so on, and
because the function a() and b() is called in the context of function
run(), the call trace will take more and more memory for the next
function call, which is like a recursive function call, and will
finailly stoped due to a function stack overflow.
the result is : the screen does print out "axaxaxax......" but the
function stack overflow does not happen.
Why???

I use x86-winXP machine. the windows taskmanager shows that the
process's memory does not increase. Why???
can anyone tell me the reason?


I tried this program on my unix machine. First I compiled it without
optimization, and it behaved as you predicted. Then I compiled it with
optimization, and it didn't eat up any memory, reflecting your experience.

My guess is that the compiler realizes what you are doing, and
implements it with gotos instead of function calls.
 
B

Ben Pfaff

jack113256 said:
In my opinion, the program will print "axaxaxa..." and so on, and
because the function a() and b() is called in the context of function
run(), the call trace will take more and more memory for the next
function call, which is like a recursive function call, and will
finailly stoped due to a function stack overflow.
the result is : the screen does print out "axaxaxax......" but the
function stack overflow does not happen.

The compiler is probably optimizing tail recursion into direct
jumps, so that each call doesn't consume stack space. Look up
"tail recursion" for more information.
 
J

jack113256

Stephen Montgomery-Smith said:
I tried this program on my unix machine.  First I compiled it without
optimization, and it behaved as you predicted.  Then I compiled it with
optimization, and it didn't eat up any memory, reflecting your experience.

My guess is that the compiler realizes what you are doing, and
implements it with gotos instead of function calls.

Yes, You are right. I used Dev-cpp(GCC) to compile my program, I've
just checked out that the default compile setting is "-O2", means
optimize level 2.
When I use "gcc test.c -O0 -otest.exe" option to compile, the compiled
program runs out of memory.

if you have GCC, you can compile the code to assembler via using
option "-S", then you can find out the object assembler code is very
different between using "-O0" and "-O2".
Thank you for your help.

:)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top