13.12 <csetjmp>

The <csetjmp> header is the C++ version 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 the functions in <csetjmp>.

jmp_buf type Jump buffer

typedef  . . .  jmp_buf;

The jmp_buf type is an opaque array type that stores information for the setjmp and longjmp functions.

longjmp function Performs nonlocal 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. When setjmp returns to its caller, it returns val; if val is 0, setjmp returns 1.

Calling longjmp is similar to throwing an exception that is caught at the point of the setjmp call. One important difference, 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. This is why you should use exceptions instead of longjmp.

setjmp function Establishes nonlocal label

int setjmp(jmp_buf env);

The setjmp function stores the current execution environment in its argument so that the environment can be restored by a call to longjmp. The first time setjmp is called, it returns 0. When longjmp is called, setjmp returns the val argument that was passed to longjmp; that value is guaranteed to be nonzero.