function
<csetjmp>

longjmp

void longjmp (jmp_buf env, int val);
Long jump
Restores the environment to the state indicated by env, evaluating the setjmp expression that filled env as val.

The function never returns to the point where it has been invoked. Instead, the function transfers the control to the point where setjmp was last used to fill the env, and evaluates the whole expression as val (unless this is zero, in which case it evaluates as value of 1).

If env was not filled by a previous call to setjmp or if the function with such call has terminated execution, it causes undefined behavior.

In C++, the implementation may perform stack unwinding that destroys objects with automatic duration. If this invokes any non-trivial destructors, it causes undefined behavior.

Parameters

env
Object of type jmp_buf filled by a previous call to setjmp that contains information to restore the environment to that point.
val
Value to which the setjmp expression evaluates.
If this is zero, the expression evaluates as 1.

Return value

none (the function never returns).

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/* longjmp example */
#include <stdio.h>      /* printf */
#include <setjmp.h>     /* jmp_buf, setjmp, longjmp */

main()
{
  jmp_buf env;
  int val;

  val=setjmp(env);

  printf ("val is %d\n",val);

  if (!val) longjmp(env, 1);

  return 0;
}

Output:
val is 0
val is 1


Data races

The scope of the setjmp and longjmp pair is limited to the current thread.

Exceptions (C++)

If no automatic objects with non-trivial destructors are involved in the potential stack unwinding, this function never throws exceptions (no-throw guarantee).
Otherwise, it causes undefined behavior.

See also