longjmp() — Restore Stack Environment
Format
#include <setjmp.h>
void longjmp(jmp_buf env, int value);
Language Level
ANSI
Threadsafe
Yes
Description
The longjmp()
function
restores a stack environment previously saved in env by
the setjmp()
function.
The setjmp()
and longjmp()
functions
provide a way to perform a non-local goto. They are often
used in signal handlers.
A call to the setjmp()
function
causes the current stack environment to be saved in env.
A subsequent call to longjmp()
restores
the saved environment and returns control to a point in the program
corresponding to the setjmp()
call.
Processing resumes as if the setjmp()
call
had just returned the given value.
longjmp()
was
called. The values of register variables are unpredictable. Nonvolatile auto variables
that are changed between calls to the setjmp()
and longjmp()
functions
are also unpredictable. setjmp()
function
does not return before you call the corresponding longjmp()
function.
Calling longjmp()
after
the function calling the setjmp()
function
returns causes unpredictable program behavior.The value argument must be nonzero.
If you give a zero argument for value, longjmp()
substitutes 1 in
its place.
Return Value
The longjmp()
function
does not use the normal function call and return mechanisms; it has
no return value.
Example
if (setjmp(mark) != 0) ...
setjmp()
function
returns a 0 when it saves the environment. The program prints the
message: setjmp has been called
The subsequent call to function p()
causes
it to call the longjmp()
function.
Control is transferred to the point in the main()
function
immediately after the call to the setjmp()
function
using the environment saved in the mark variable. This time,
the condition is TRUE because -1 is specified in the second
parameter on the longjmp()
function
call as the return value to be placed on the stack. The example then
performs the statements in the block, prints the message "longjmp()
has
been called", calls the recover()
function,
and leaves the program.
#include <stdio.h>
#include <setjmp.h>
#include <stdlib.h>
jmp_buf mark;
void p(void);
void recover(void);
int main(void)
{
if (setjmp(mark) != 0)
{
printf("longjmp has been called\n");
recover();
exit(1);
}
printf("setjmp has been called\n");
printf("Calling function p()\n");
p();
printf("This point should never be reached\n");
}
void p(void)
{
printf("Calling longjmp() from inside function p()\n");
longjmp(mark, -1);
printf("This point should never be reached\n");
}
void recover(void)
{
printf("Performing function recover()\n");
}
/*******************Output should be as follows: **********************
setjmp has been called
Calling function p()
Calling longjmp() from inside function p()
longjmp has been called
Performing function recover()
**********************************************************************/