#define setjmp(env) /* implementation-defined */
Saves the current execution context into a variable env
of type jmp_buf. This variable can later be used to restore the current execution context by longjmp function. That is, when a call to longjmp function is made, the execution continues at the particular call site that constructed the jmp_buf variable passed to longjmp. In that case setjmp
returns the value passed to longjmp.
The invocation of setjmp
must appear only in one of the following contexts:
switch(setjmp(env)) { // ...
if(setjmp(env) > 10) { // ...
while(!setjmp(env)) { // ...
void
).If setjmp
appears in any other context, the behavior is undefined.
Upon return to the scope of setjmp
:
setjmp
, whose values are indeterminate if they have been changed since the setjmp
invocation.â0â if the macro was called by the original code and the execution context was saved to env
.
Non-zero value if a non-local jump was just performed. The return value is the same as passed to longjmp.
[edit] NotesAbove requirements forbid using return value of setjmp
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; noreturn void 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) { volatile int count = 0; // modified local vars in setjmp scope must be volatile if (setjmp(my_jump_buffer) != 5) // compare against constant in an if foo(++count); }
Output:
foo(1) called foo(2) called foo(3) called foo(4) called[edit] References
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4