setjmp() saves the current calling environment (address in the C runtime stack, program counter, register contents) in its env argument for later use by the longjmp() function. setjmp() is implemented as a macro in the POSIX subsystem; it may be implemented as a function in other X/Open-conformant systems.
If a macro definition is suppressed in order to access an existing function, or defines a program or an external identifier with the name setjmp , the behavior is undefined. setjmp() is only meaningful in combination with the longjmp() function: these two functions can be combined to implement non-local jumps, i.e. jumps from any given function to any other active function. A longjmp call restores the calling environment saved by setjmp() and then resumes program execution (see also longjmp() ).
env is the array in which setjmp() stores the current program state. The type jmp_buf is defined in the header setjmp.h . All accessible objects will have the same values as when longjmp() was called, except for the values of "automatic" objects, which are undefined under the following conditions: They are local to the function containing the corresponding setjmp call. They are not of type volatile . They are changed between the setjmp and longjmp calls.
setjmp() should only be used in one of the following contexts:
as the entire controlling expression of a selection or iteration statement, e.g.: if (setjmp(env)) ...
as one operand of a relational operator with the other operand an integral constant expression, with the resulting expression being the entire controlling expression of a selection or iteration statement, e.g.: if (setjmp(env) == 0) ...
as the operand of a unary "!" operator with the resulting expression being the entire controlling expression of a selection or iteration statement, e.g.: if (!setjmp(env)) ...
as the entire expression of an expression statement (possibly cast to void ): void: (void)setjmp(env);
|