| Program termination | |||||||||||||||||||||
| |||||||||||||||||||||
| Unreachable control flow | |||||||||||||||||||||
(C23) | |||||||||||||||||||||
| Communicating with the environment | |||||||||||||||||||||
| |||||||||||||||||||||
| Memory alignment query | |||||||||||||||||||||
(C23) | |||||||||||||||||||||
| Signals | |||||||||||||||||||||
| Signal types | |||||||||||||||||||||
| Non-local jumps | |||||||||||||||||||||
| |||||||||||||||||||||
| Types | |||||||||||||||||||||
Defined in header <setjmp.h> | ||
#define setjmp(env) /* implementation-defined */ | ||
Saves the current execution context into a variableenv of typejmp_buf. This variable can later be used to restore the current execution context bylongjmp function. That is, when a call tolongjmp function is made, the execution continues at the particular call site that constructed thejmp_buf variable passed tolongjmp. In that casesetjmp returns the value passed tolongjmp.
The invocation ofsetjmp must appear only in one of the following contexts:
switch(setjmp(env)){// ...
if(setjmp(env)>10){// ...
while(!setjmp(env)){// ...
void).setjmp(env);
Ifsetjmp appears in any other context, the behavior is undefined.
Upon return to the scope ofsetjmp:
setjmp, whose values are indeterminate if they have been changed since thesetjmp invocation.Contents |
| env | - | variable to save the execution state of the program to. |
0 if the macro was called by the original code and the execution context was saved toenv.
Non-zero value if a non-local jump was just performed. The return value is the same as passed tolongjmp.
Above requirements forbid using return value ofsetjmp in data flow (e.g. to initialize or assign an object with it). The return value can only be either used in control flow or discarded.
#include <stdio.h>#include <setjmp.h>#include <stdnoreturn.h> jmp_buf my_jump_buffer; noreturnvoid foo(int status){printf("foo(%d) called\n", status);longjmp(my_jump_buffer, status+1);// will return status+1 out of setjmp} int main(void){volatileint count=0;// modified local vars in setjmp scope must be volatileif(setjmp(my_jump_buffer)!=5)// compare against constant in an if foo(++count);}
Output:
foo(1) calledfoo(2) calledfoo(3) calledfoo(4) called
| jumps to specified location (function)[edit] | |
C++ documentation forsetjmp | |