setjmp()

QNX SDP8.0C Library ReferenceAPIDeveloper

Save the calling environment for longjmp()

Synopsis:

#include <setjmp.h>

int setjmp( jmp_buf env );

Arguments:

env
A buffer where the function can save the calling environment.

Library:

libc

Use the -l c option to qcc to link against this library. This library is usually included automatically.

Description:

The setjmp() function saves the calling environment in its env argument for use by the longjmp() function.

Error handling can be implemented by using setjmp() to record the point to return to following an error. When an error is detected in a function, that function uses longjmp() to jump back to the recorded position. The original function that called setjmp() must still be active (that is, it can't have returned to the function that called it).

Be careful to ensure that any resources (allocated memory, opened files, etc.) are cleaned up properly.

In QNX OS, setjmp() does not save the thread's signal mask and longjmp() does not restore it. If you need to save and restore the thread's signal mask, use sigsetjmp() and siglongjmp() instead.

Warning:
Don't use longjmp() or siglongjmp() to restore an environment saved by a call to setjmp() or sigsetjmp() in another thread. If you're lucky, your application will crash; if not, it'll look as if it works for a while, until random scribbling on the stack causes it to crash.
CAUTION:

Our current setjmp() macro implementation evaluates its argument more than once. Do not pass an expression with side effects as its argument because unexpected behavior may result.

You may call setjmp() only in one of the following contexts:
  • The entire controlling expression of a selection or iteration statement
  • One operand of a relational or equality operator with the other operand being an integral constant expression, and with the resulting expression being the type described above for the first context
  • The operand of a unary ! operator with the resulting expression being the type described above for the first context
  • The entire expression of an expression statement (possibly cast to void)
If called in any other context, this function behaves in an undefined way.

Returns:

Zero if invoked directly, or nonzero if the return is the result of a call to longjmp().

Examples:

See longjmp().

Classification:

ANSI, POSIX 1003.1

Safety:
Cancellation pointNo
Signal handlerYes
ThreadYes
Page updated: