473,804 Members | 4,269 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Callback Function Stack Overflow

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?
Dec 27 '07 #1
3 2899
jack113256 wrote:
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.
Dec 27 '07 #2
jack113256 <hw*******@gmai l.comwrites:
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.
--
"Large amounts of money tend to quench any scruples I might be having."
-- Stephan Wilms
Dec 27 '07 #3
Stephen Montgomery-Smith <step...@missou ri.eduwrote:
jack113256 wrote:
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.
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.

:)
Dec 27 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
7145
by: scott ocamb | last post by:
hello I have implemented a solution using async methods. There is one async method that can be invoked multiple times, ie there are multiple async "threads" running at a time. When these threads are complete, the call the Callback method. Each "thread" calls the same callback method. What thread does this callback method exist on? My testing indicates the
3
3416
by: Tomaz Rotovnik | last post by:
Hi I created very simple dll (vc++) which has three functions (start, stop and initialization). it starts capturing sound from soundblaster and when the buffer is filled with the data, dll calls VB calback function (passed with initialization) and passes the buffer size (integer type). First I have some problems with __stdcall declaration in VC dll, but I think I solved that problem. Callback function runs in separated thread under main...
19
3146
by: Jim | last post by:
I have spent the past few weeks designing a database for my company. The problem is I have started running into what I believe are stack overflow problems. There are two tab controls on the form (nested), three list views, one tree control with up to 30,000 nodes, maybe 15 comboboxes (half of which have a large recordset as rowsource), 20 or so buttons and around 30 text boxes (not to mention the images, labels, etc and around 1000 lines...
4
9056
by: Victor | last post by:
Hello, I've got a situation in which the number of (valid) recursive calls I make will cause stack overflow. I can use getrlimit (and setrlimit) to test (and set) my current stack size. However, it is not as straightforward to determine the base address for my stack space. The approach I have taken is to save the address of an automatic variable in main( ), and assume this is a fairly good indicator of my base address. Then, I can...
1
17050
by: Timbo | last post by:
Hi all, This is my first message here so i'll try and include all the information that will help you help me out, if possible. Basically I am using C# in ASP.NET 2.0 and have a Repeater control in my aspx page with two image buttons, one for an edit command, another a delete command. Here is a cut down code fragment. ...
7
3599
by: Kirk McDonald | last post by:
Let's say I have a function that takes a callback function as a parameter, and uses it to describe an iteration: def func(callback): for i in : callback(i) For the sake of argument, assume the iteration is something more interesting than this which relies on the callback mechanism. The function is an existing interface, and I cannot change it.
7
22052
by: amit.atray | last post by:
Environement : Sun OS + gnu tools + sun studio (dbx etc) having some Old C-Code (ansi + KR Style) and code inspection shows some big size variable (auto) allocated (on stack) say for ex. char a; (this type of code and other complex mallc/free etc is used frequently and total approx 450 files, each file is of approx/avg 1000 line,
1
3113
by: George2 | last post by:
Hello everyone, Such code segment is used to check whether function call or exception- handling mechanism runs out of memory first (written by Bjarne), void perverted() { try{
5
4323
by: Jef Driesen | last post by:
I have a C DLL that I want to use from a C# project. The C header file contains these declarations: typedef void (*callback_t) (const unsigned char *data, unsigned int size, void *userdata); void myfunction (callback_t callback, void *userdata); How do I translate this to C#? I tried with:
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10575
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10076
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9144
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6851
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5520
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4297
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3816
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.