Setjmp/longjmp
The setjmp/longjmp mechanism is a C nonlocal-control-flow facility that saves an execution context and later restores it to transfer control across function-call boundaries.
Modern C, Third Edition
A long jump is a nonlocal control transfer that can move execution across function-call boundaries.
- How it is used
- Handles exceptional conditions across nested function calls without returning through each call.
- How deep it goes
- Works through a recursive parser and states target-validity, macro-placement, and volatile constraints without detailing jump-buffer internals.
- Examples
- Uses a recursive parser whose output failure triggers `longjmp` and whose outer `switch` handles the resulting condition.
- What you can do after
- Apply it via C’s `<setjmp.h>` interface to route exceptional nested-call failures to a valid recovery target.
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.
- How it is used
- Handles exceptional conditions detected across nested calls of a recursive parser.
- 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 you can do after
- Apply it via setjmp.h to route exceptional conditions from nested calls to one handling point.
Rust in Action: Systems programming concepts and techniques
longjmp is a non-local control-flow function or compiler intrinsic that restores an execution context previously saved by setjmp.
- How it is used
- Handles low-level errors, interrupts, or shutdown paths that must escape deeply nested call stacks.
- How deep it goes
- Explains saved contexts, skipped stack frames, recursive behavior, and LLVM declarations, but stops short of full intrinsic implementation details.
- Examples
- Uses recursive dive() calls, depth-marker output, a saved jump buffer, signal constants, and a shutdown flag.
- What you can do after
- Build it from scratch as a non-Windows Rust project that invokes LLVM's setjmp and longjmp facilities through unsafe declarations.
Read how Rust in Action: Systems programming concepts and techniques explains setjmp/longjmp →
Related concepts
- jmp_bufuses · jmp_buf is an opaque C library-defined type representing a saved execution environment for setjmp and longjmp.
- macrokind of · A macro is a named construct that expands or transforms source code, commands, or templates before or during execution.
- exceptionaddresses · An exception is a runtime signal or object representing an abnormal condition that interrupts ordinary program flow and may be handled or propagated.
- performance optimizationcontrasts with · Performance optimization is the systematic modification of software or its operating conditions to improve measured execution speed, responsiveness, throughput, or resource efficiency while preserving required behavior.
- stack frameuses · A stack frame is a runtime record that holds the local context and execution state for one active function or method invocation.
- compiler intrinsickind of · A compiler intrinsic is a compiler-recognized language construct exposing a low-level operation through a special implementation rather than ordinary source code.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.


