7.4.9 <setjmp.h>

Supports transfer of control between functions.
The following macros are implementation-defined.

Type

Definition Name

Description

Type
(macro)

jmp_buf

Indicates the type name corresponding to a storage area for storing information that enables transfer of control between functions.

Function

setjmp

Saves the execution environment defined by jmp_buf of the currently executing function in the specified storage area.

longjmp

Restores the function execution environment saved by the setjmp function, and transfers control to the program location at which the setjmp function was called.

The setjmp function saves the execution environment of the current function. The location in the program that called the setjmp function can subsequently be returned to by calling the longjmp function.

An example of how transfer of control between functions is supported using the setjmp and longjmp functions is shown below.

[Format]

  1    #include <stdio.h>
  2    #include <setjmp.h>
  3    jmp_buf env;
  4    void sub( );
  5    void main( )
  6    {
  7
  8            if (setjmp(env)!=0){
  9                 printf("return from longjmp\n");
  10                exit(0);
  11            }
  12            sub( );
  13   }
  14
  15    void sub( )
  16   {
  17          printf("subroutine is running \n");
  18          longjmp(env, 1);
  19   }

 

Explanation:

The setjmp function is called in line 8. At this time, the environment in which the setjmp function was called is saved in jmp_buf type variable env. The return value in this case is 0, and therefore function sub is called next.

The environment saved in variable env is restored by the longjmp function called within function sub. As a result, the program behaves just as if a return had been made from the setjmp function in line 8. However, the return value at this time is 1 specified by the second argument of the longjmp function. As a result, execution proceeds to line 9.

setjmp

Saves the execution environment of the currently executing function in the specified storage area.

[Format]

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

[Parameters]

env Pointer to storage area in which execution environment is to be saved

[Return values]

When setjmp function is called: 0

On return from longjmp function: Nonzero

[Remarks]

The execution environment saved by the setjmp function is used by the longjmp function. The return value is 0 when the function is called as the setjmp function, but the return value on return from the longjmp function is the value of the second parameter specified by the longjmp function.

If the setjmp function is called from a complex expression, part of the current execution environment, such as the intermediate result of expression evaluation, may be lost. The setjmp function should only be used in the form of a comparison between the result of the setjmp function and a constant expression, and should not be called within a complex expression.

Do not call the setjmp function indirectly using a pointer.

 

longjmp

Restores the function execution environment saved by the setjmp function, and transfers control to the program location at which the setjmp function was called.

[Format]

#include <setjmp.h>
void longjmp (jmp_buf env, long ret);

[Parameters]

env Pointer to storage area in which execution environment was saved

ret Return code to setjmp function

[Remarks]

From the storage area specified by the first parameter env, the longjmp function restores the function execution environment saved by the most recent invocation of the setjmp function in the same program, and transfers control to the program location at which that setjmp function was called. The value of the second parameter ret of the longjmp function is returned as the setjmp function return value. However, if ret is 0, the value 1 is returned to the setjmp function as a return value.

If the setjmp function has not been called, or if the function that called the setjmp function has already executed a return statement, the operation of the longjmp function is not guaranteed.