Callback Function Stack Overflow

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jack113256

    #1

    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?
  • Stephen Montgomery-Smith

    #2
    Re: Callback Function Stack Overflow

    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.

    Comment

    • Ben Pfaff

      #3
      Re: Callback Function Stack Overflow

      jack113256 <hwh64ster@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

      Comment

      • jack113256

        #4
        Re: Callback Function Stack Overflow

        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.

        :)

        Comment

        Working...