liveBook · Manning

Setjmp/longjmp

As explained in Modern C

setjmp is a C library macro that saves an execution context and returns zero initially or a nonzero value when control later returns through longjmp.

A parser’s escape route

A recursive parser checks for unmatched braces, excessive nesting, output failure, and interruption. Its outer function declares a jmp_buf and switches on setjmp(jmpTarget). Case 0 calls descend. If descend detects an exceptional condition, it calls longjmp(jmpTarget, condition); control reaches the switch again, now selecting the corresponding warning or error. A failed newline write, for example, calls longjmp(jmpTarget, eofOut) rather than continuing to parse. section 17.5

setjmp establishes a target; longjmp transfers control back to it across nested function calls. The target is an opaque jmp_buf whose contents must still be valid when used. Unlike a goto label, the target can be used from another function. setjmp returns zero when first reached and a nonzero result after the jump, allowing one switch to distinguish normal execution from exceptional outcomes. section 17.5

Figure 17.2. Control flow with setjmp and longjmp: longjmp jumps to the position marked by setjmp.

Compare an ordinary function return with the jump back to the position marked by setjmp. section 17.5

Two details matter when following this control flow:

  • longjmp never returns to its caller. Once an error branch calls it, statements later in that call do not execute; the compiler can use that fact when analyzing the remaining branches. section 17.5
  • A jump can bypass ordinary cleanup on the way out of recursive calls. A depth value that would normally be restored before return may therefore remain changed. This interaction with optimization is why some variables need volatile qualification. section 17.4 section 17.7

TAKEAWAY: setjmp/longjmp provides one destination for exceptional conditions detected deep in a call chain, but the jump is not a sequence of normal returns. Check both the target’s validity and any state that ordinary returns would have restored. section 17.5 section 17.4

How Modern C covers it

How deep it goes
Works through jump-target setup, return paths, and optimization consequences; defers detailed treatment of asynchronous signals.
Examples
Traces a recursive brace parser that reports unmatched braces, excessive nesting, output failure, or interruption.
What it assumes
Assumes familiarity with C function calls and local variables while introducing jmp_buf and its jump behavior.
What you can do after
Apply it via setjmp.h to route exceptional conditions from nested calls to one handling point.
How it connects
Connects recursive function calls, goto labels, compiler optimization, volatile qualification, and signal handling.

Also explained in

Compare how 3 books explain setjmp/longjmp →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C. The section links point to the parts of the book it draws on.