Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

setjmp - set label for non-local jump

&pagelevel(4)&pagelevel

Syntax

#include <setjmp.h>

int setjmp(jmp_buf env);

Description

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);

Return val.

0

on successful return from a direct invocation of sigset.


!= 0

if the return is from a call to longjmp(). In this case the return value corresponds to the value of the val argument of the longjmp call.

Notes

In general, sigsetjmp() is more suitable than setjmp() for handling errors and signals which occur in low-level subroutines. 

See also

longjmp(), sigsetjmp(), setjmp.h.