Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Borland C++ 2.x ( with Turbo C ) - <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() and its companion routine setjmp() provide a method of
    executing a non-local goto. They are typically used to pass control
    to error-handling code in a previously called routine, without using
    the normal calling conventions.

    longjmp() restores a stack environment previously saved (in 'env') by
    a call to setjmp() and returns control to the point just after the
    setjmp call.  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 contain the
    values they had when longjmp() was called.

       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.

                    longjmp() must be called before the routine that
                    called setjmp() 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