liveBook · Manning

Jmp_buf

As explained in Modern C, Third Edition

jmp_buf is an opaque C type for storing the execution environment used by setjmp and longjmp.

jmp_buf is an opaque C object type used by setjmp and longjmp to preserve a jump target. Unlike a goto label, it may be declared outside the function containing the jump, provided it remains alive and valid. section 19.5

Parser control-flow interface

A local jmp_buf can surround a recursive descent operation: switch (setjmp(jmpTarget)) { case 0: descend(..., jmpTarget); break; case error: /* report condition */ break; }. Normal control flow selects case 0; longjmp transfers execution back to setjmp, which then produces the condition value. section 19.5

Remember these validity rules:

  • Keep the jmp_buf's declaration scope active when calling longjmp. section 19.5
  • Preserve the complete setjmp context; leaving its scope invalidates the jump target. section 19.5
  • Treat longjmp as nonreturning; control resumes at setjmp rather than at the longjmp call. section 19.5
Figure 19.2 Control flow with setjmp and longjmp: longjmp jumps to the position marked by setjmp.
figure

Trace how longjmp returns control to the position marked by setjmp. section 19.5

How Modern C, Third Edition covers it

How deep it goes
Explains opacity, normal setjmp return, longjmp transfer, lifetime, validity, and nonreturning behavior, but not representation internals.
Examples
Uses a recursive-descent parser, a switch dispatcher, and an end_line function that calls longjmp on output failure.
What it assumes
Assumes familiarity with C functions, scopes, switch statements, and control flow; defines nonlocal jump behavior from concrete parser code.
What you can do after
Apply jmp_buf with setjmp and longjmp while preserving the jump target's scope and execution context.
How it connects
Connects jmp_buf with setjmp, longjmp, goto, recursive descent, and switch-based error handling.

Also explained in

Compare how 2 books explain jmp_buf →

Related concepts

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