liveBook · Manning

Jmp_buf

As explained in Modern C

jmp_buf is an opaque C library-defined array type that represents a saved execution context for setjmp and longjmp.

jmp_buf is an opaque C library-defined object type that represents a saved execution context for setjmp and longjmp. Declare it in a scope that remains active while the jump target is used. section 17.5

A parser jump target

void basic_blocks(void) { jmp_buf jmpTarget; switch (setjmp(jmpTarget)) { case 0: descend(jmpTarget); break; case eofOut: puts("output failed"); break; } } Normal control flow reaches case 0 because setjmp returns zero. A deeper function can call longjmp(jmpTarget, eofOut), transferring control back to the setjmp location, where the nonzero condition selects an alternative case.

Preserve these rules when using a jmp_buf:

  • Keep the object alive: leaving the scope of the setjmp call invalidates its jump target. section 17.5
  • Keep the saved context active when longjmp is called; a local declaration in the same scope avoids complications. section 17.5
  • Treat longjmp as nonreturning: execution does not continue in its caller after the jump. section 17.5

How Modern C covers it

How deep it goes
Explains declaration, normal setjmp return, nonlocal transfer, and scope validity, while deferring implementation details of the opaque object.
Examples
Demonstrates a recursive descent parser using a switch, helper-function longjmp calls, and distinct exceptional-state values.
What you can do after
Apply it via setjmp and longjmp to centralize exceptional control flow without continuing execution in the jumping caller.
How it connects
Connects jmp_buf with setjmp, longjmp, recursive descent parsing, and nonlocal control flow.

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. The section links point to the parts of the book it draws on.