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 would typically be 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);
}
Seealso:
This page last updated on Fri Nov 30 10:48:32 MSK 2001
Copyright © 1992-2001, Vitaly Filatov, Moscow, Russia
Webmaster