HP C/iX Library Reference Manual (30026-90004)

236 Chapter5
HP C/iX Library Function Descriptions
longjmp
longjmp
Restores an environment previously saved by setjmp().
Syntax
#include <setjmp.h>
void longjmp (jmp_buf
env
, int
val
);
Parameters
env
Passes information needed to restore a previous environment. This
variable was used in a previous call to setjmp() to save the environment.
The type jmp_buf (defined in <setjmp.h>) defines an array of unsigned
integers. For this reason, the
env
argument does not require an & operator.
val
Passes a value to be returned by setjmp(). If a zero is passed in this
argument, it is changed to a value of 1 to ensure that longjmp() never
causes setjmp() to return a zero value.
Return Values
None. Control is returned to the program at the statement following the call to setjmp.
Description
The longjmp function restores an environment saved in the
env
argument by a previous
call to setjmp().Ifthe
env
argument is not the result of a successful call to setjmp(), the
operation of longjmp() is undefined and usually results in the program aborting.
After the call to longjmp() completes, the program executes as if the call to setjmp()
(which stored information into the
env
argument) has returned a second time. The result
of the second return from setjmp() is the return of the value of the nonzero
val
argument
supplied to longjmp().
The calling environment defined in
env
is restored by longjmp(). This includes trimming
the stack so that all stack frames beyond the frame marked by
env
are removed.
The longjmp function cannot add stack frames. This means that if a sequence of functions
is:
A == calls ==> B == calls ==> C
and setjmp() is used in function C to save an environment in a global
env
, functions B or
A may not contain any longjmp() calls that reference the
env
values. Only subordinate
functions may issue calls to longjmp(). As a special case, a function may issue a
longjmp() call that references a setjmp() within itself, although this is not usually done.
The longjmp function works correctly in the context of signals and interrupts and any of
their associated functions. However, the effects of invoking longjmp() from a nested
signal handler (that is, a function invoked as a result of a signal raised while handling
another signal) are undefined.