Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Microsoft C 6.0 - <b>longjmp() restore program state</b> http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
 longjmp()               Restore Program State

 #include   <setjmp.h>

 void       longjmp(env,value);
 jmp_buf    env;                         Variable environment is stored in
 int        value;                       Value to be returned to setjmp()

    longjmp(), with its companion routine setjmp(), provides a means of
    executing a non-local goto. longjmp() restores a stack environment
    previously saved (in 'env') by a call to setjmp().  It returns
    control to the point just after the setjmp call, and execution of the
    program continues as if setjmp() returned with the given 'value'.
    The values of all variables (except register variables) accessible to
    the calling routine are the same as when setjmp() was called.

    longjmp() and setjmp() would typically be used to bypass normal
    calling conventions while passing control to error-handling code in a
    previously called routine.

    Returns:    The longjmp() function itself does not return a value. It
                "feeds" its argument, 'value', to setjump, which returns
                'value'.

      Notes:    The values of register variables may not be restored
                correctly.

                A routine that calls setjmp() must also call longjmp()
                before it terminates. If not, the results are
                unpredictable.

                'value' must be nonzero. If 'value' is set to 0, the
                value 1 is returned instead to setjmp().

   -------------------------------- Example ---------------------------------

    The following statements use setjmp() and longjmp() to save and
    restore a stack environment.

           #include <stdio.h>
           #include <setjmp.h>

           int val;
           jmp_buf jump;

           main()
           {
               val = setjmp(jump);
               if (val != 0)
               {
               printf("longjmp called. control returned to setjmp. val:
                        %d\n",val);
               exit(val);
               }
               printf("setjmp first called, val is %d\n",val);
               s();
           }

           s()
           {
               printf("subroutine called\n");
               longjmp(jump,1);
           }


See Also: setjmp()

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson