Callback Function Stack Overflow

Discussion in 'C Programming' started by jack113256, Dec 27, 2007.

  1. jack113256

    jack113256 Guest

    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?
    jack113256, Dec 27, 2007
    #1
    1. Advertising

  2. 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.
    Stephen Montgomery-Smith, Dec 27, 2007
    #2
    1. Advertising

  3. jack113256

    Ben Pfaff Guest

    jack113256 <> writes:

    > 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
    Ben Pfaff, Dec 27, 2007
    #3
  4. jack113256

    jack113256 Guest

    Stephen Montgomery-Smith <> wrote:
    > 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.

    :)
    jack113256, Dec 27, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?amJpeEBuZXdzZ3JvdXBzLm5vc3BhbQ==?=

    Stack overflow exception

    =?Utf-8?B?amJpeEBuZXdzZ3JvdXBzLm5vc3BhbQ==?=, Apr 20, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    7,234
    Rick Spiewak
    Apr 22, 2004
  2. Mr m?ll
    Replies:
    2
    Views:
    1,394
    Mr m?ll
    Oct 16, 2004
  3. Victor
    Replies:
    4
    Views:
    581
    James Hu
    Sep 23, 2003
  4. Jim Lambert

    Stack overflow in function ...

    Jim Lambert, Jan 15, 2004, in forum: C Programming
    Replies:
    12
    Views:
    524
    Jim Lambert
    Jan 15, 2004
  5. Kenneth McDonald

    Why stack overflow with such a small stack?

    Kenneth McDonald, Aug 30, 2007, in forum: Ruby
    Replies:
    7
    Views:
    246
    Kenneth McDonald
    Sep 1, 2007
Loading...

Share This Page