The <csetjmp>
header is a mapping of the C standard <setjmp.h>
header. This chapter presents only the most cursory description of this header because its use is limited in a C++ program. Use exceptions instead of using the functions in <csetjmp>
.
Jump buffer
typedef opaque jmp_buf;
The jmp_buf
type is an opaque array type that stores information for the setjmp
and longjmp
functions.
Non-local goto
void longjmp(jmp_buf env, int val);
The longjmp
function bypasses the normal function return and unwinds the call stack to the point where setjmp was called with the same jmp_buf
environment.
Calling longjmp
is similar to throwing an exception that is caught at the point of the setjmp
call. One important different, however, is that if any objects on the stack would have been destroyed by throwing an exception, the program's behavior is undefined if you call longjmp
. That's why you should use exceptions instead of longjmp
.
Non-local label
int setjmp(jmp_buf env);
The setjmp
function stores the current execution environment in its argument, so that environment can be restored by a call to longjmp
. The first time setjmp
is called, it returns zero. When longjmp
is called, the parameter to longjmp
is returned from setjmp
; that value is guaranteed to be non-zero.