gtpc2m4m | C/C++ Language Support User's Guide |
This function restores a stack environment previously saved in env by the setjmp function. The setjmp and longjmp functions provide a way to perform a nonlocal 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 the longjmp function restores the saved environment and returns control to a point in the program corresponding to the setjmp call. The program resumes as if the setjmp call had just returned the given value of the value argument. The setjmp and longjmp functions are not restricted to the same dynamic load module (DLM).
Format
#include <setjmp.h> void longjmp(jmp_buf env, int value);
Normal Return
Void.
Error Return
Error conditions exist that would cause the process to exit with a SNAPC dump; the ones that can be detected by the longjmp function are:
Programming Considerations
Examples
This example provides for saving the stack environment at the following statement: if(setjmp(mark) != 0) ...
When the example first performs the if statement, it saves the environment in mark and sets the condition to FALSE because the 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 tests for a local error condition, which can cause it to perform the longjmp function. (The function p that performs the longjmp function can be in the same DLM or a separate DLM). Control then returns to the original setjmp function using the environment saved in mark. This time the condition is TRUE because -1 is the returned value from the longjmp function. The example then performs the statements in the block and prints: longjmp has been called.
It then performs the recover function and leaves the program.
/* Illustration of longjmp(). */ #include <stdio.h> #include <setjmp.h> jmp_buf mark; void p(void); void recover(void); int ADLM(void) { if (setjmp(mark) != 0) { printf("longjmp has been called\n"); recover(); return(1); } printf("setjmp has been called\n");
·
·
·
p();
·
·
·
return(32); /* Return suitable value */ } void p(void) { int error = 0;
·
·
·
error = 9;
·
·
·
if (error != 0) longjmp(mark, -1);
·
·
·
} void recover(void) {
·
·
·
}
Related Information