setjmp

Is a macro that saves its calling environment in the env parameter for subsequent use by the longjmp function.

Library:LibC
Classification:ANSI
Service:General C Services

Syntax

  #include <setjmp.h> 
   
  int setjmp (
     jmp_buf   env); 
  

Parameters

env

(OUT) Specifies the buffer in which to save environment.

Return Values

This macro returns a value of 0 when it is initially called. The return value is nonzero if the return is the result of a call to the longjmp function. An if statement is often used to handle these two returns. When the return value is 0, the initial call to setjmp has been made; when the return value is nonzero, a return from a longjmp has just occurred.

Remarks

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

Special care must be exercised to ensure that any side effects that have occurred (such as allocated memory and opened files) are satisfactorily handled.

For sample code, see ThrJmp.c.

See Also

longjmp