Skip to content

Non-local jumps (setjmp.h)

Include setjmp.h to save a stack context and return to it later with a non-local jump.

Types and Macros

Name Meaning
jmp_buf Saved non-local jump context for setjmp/longjmp.

Functions

Function Summary
_Noreturn void longjmp(jmp_buf env, int val) Restore a context saved by setjmp and make that setjmp return val, or 1 if val is 0.
int setjmp(jmp_buf env) Save the current execution context and return 0 on the direct call.

Runtime model

jmp_buf is an 8-byte buffer holding the saved return address, stack pointer, IX frame pointer, and padding. DCC C Compiler declares setjmp as an ordinary function; the runtime entry still reads the caller frame so the call behaves like the C89 non-local jump primitive.

setjmp(env) returns 0 when the context is saved directly. A later longjmp(env, val) restores that context and makes the saved setjmp return val, or 1 if val is 0.

Non-local jump pattern

Keep the jmp_buf alive until every possible longjmp using it is finished. File-scope or caller-owned storage is safest:

#include <setjmp.h>

jmp_buf env;

void fail(void)
{
    longjmp(env, 7);
}

int main(void)
{
    int code;

    code = setjmp(env);
    if (code == 0) {
        fail();
    } else {
        return code;
    }
    return 0;
}

After a longjmp, automatic variables in the restored function have the same practical caveats as C89: values changed after setjmp should not be relied on unless they are stored in stable storage.