Guardian C Library Calls Reference Manual
longjmp
3-108 128833—Guardian TNS C Library Calls Reference Manual
Reference to Library Calls
longjmp
The longjmp function restores the calling environment saved by the most recent call to
setjmp.
env
is a buffer of type jmp_buf used in an earlier call to setjmp.
val
specifies the value longjmp is to cause its corresponding setjmp to return. If val is
zero, the setjmp call returns the value 1, as zero is reserved by the run-time library.
Return Value
none. However, after the call to longjmp, program execution resumes as if the
corresponding setjmp call had just returned the value val.
Usage Guidelines
•
To restore the calling environment (in this context) means to restore the stack to the
way it was when setjmp was called. If a function f calls setjmp and then calls
function g1 which calls g2 which calls g3, then when longjmp is called from g3,
longjmp removes from the stack all information (local variables, return addresses,
and so forth) due to calls to functions g1 through g3. The environment buffer saves
the information needed for stack restoration.
•
If you do not initialize env before calling longjmp, or if you call longjmp after the
function containing its corresponding setjmp has completed execution, the result of
the call to longjmp is unpredictable. If it can detect the error, longjmp returns to its
caller.
•
The call to setjmp and all calls to longjmp with the same environment buffer resume
execution at the same place: following the call to setjmp. You can distinguish
between these different cases only if you pass different values of val. That is,
execution resumes at the same place, but with different return values.
•
In order for the calling environment to be properly restored, you must call setjmp
from simple expressions only. An expression is simple if the setjmp call is the sole
term, or if the result of the setjmp call is compared with a constant expression.
#include <setjmph>
void longjmp(jmp_buf env, int val);