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>setjmp() save program state</b> http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
 setjmp()                Save Program State

 #include   <setjmp.h>

 int        setjmp(env);
 jmp_buf    env;                         Variable environment is stored in

    setjmp(), with its companion routine longjmp(), provides a means of
    executing a non-local goto.  setjmp() saves the current stack
    environment in 'env'. The stack environment is restored by a
    subsequent call to longjmp(), and control returns to the point just
    after the setjmp() call.  The values of all variables (except
    register variables) accessible to the calling routine are the same as
    when longjmp() was called.

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

    Returns:    0 after saving the stack environment. If setjmp() returns
                because of a subsequent longjmp() call, it returns the
                'value' argument from the longjmp().  (The 'value'
                argument cannot be 0).  If the 'value' argument from
                longjmp() == 1, then setjmp() returns 0.  No error
                return.

      Notes:    The values of register variables in the routine calling
                setjmp() may not be restored correctly after the
                longjmp().

   -------------------------------- 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: longjmp()

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