std::jmp_buf(3)
π std::jmp_buf β bestie edition
okay so first things first: this page lives in the C++ Standard Library manual (section 3, std::jmp_buf(3)), and it's about one very specific little typedef that's lowkey the backbone of old-school error handling. we're gonna get through it together, i promise π«Ά
π NAME
std::jmp_buf β literally just std::jmp_buf, that's the whole name, slay
β¨ Synopsis
Okay so picture this: it's defined in the header <csetjmp, and it looks like:
typedef /* unspecified */ jmp_buf;
That's it. That's the whole thing. The type is /* unspecified */, which means the standard is being coy on purpose β nobody's gonna tell you exactly what it is, and honestly? iconic of them. π
The std::jmp_buf type is an array type suitable for storing information to restore a calling environment. Like, it's a little memory box where you stash the vibe of "where we were" so you can teleport back there later. π
And the stored info is sufficient to restore execution at the correct block of the program and invocation of that block. So it knows where to put you back. It's giving GPS for your stack, fr fr.
β οΈ but bestie listen β the state of floating-point status flags, or open files, or any other data is NOT stored in an object of type std::jmp_buf. it's just the location info, not your whole life. don't come for me when your file descriptors are still open, i warned you π
πΈ Example
okay this one's actually such a cute lil demo β a quadratic equation solver that bails out to an error handler when the discriminant goes negative. "Run this code" as the manpage says, and watch the magic β¨
#include <array>
#include <cmath>
#include <csetjmp>
#include <cstdlib>
#include <format>
#include <iostream>
std::jmp_buf solver_error_handler;
std::array<double, 2> solve_quadratic_equation(double a, double b, double c)
{
const double discriminant = b * b - 4.0 * a * c;
if (discriminant < 0)
std::longjmp(solver_error_handler, true); // Go to error handler
const double delta = std::sqrt(discriminant) / (2.0 * a);
const double argmin = -b / (2.0 * a);
return {argmin - delta, argmin + delta};
}
void show_quadratic_equation_solution(double a, double b, double c)
{
std::cout << std::format("Solving {}xΒ² + {}x + {} = 0...\n", a, b, c);
auto [x_0, x_1] = solve_quadratic_equation(a, b, c);
std::cout << std::format("xβ = {}, xβ = {}\n\n", x_0, x_1);
}
int main()
{
if (setjmp(solver_error_handler))
{
// Error handler for solver
std::cout << "No real solution\n";
return EXIT_FAILURE;
}
for (auto [a, b, c] : {std::array{1, -3, 2}, {2, -3, -2}, {1, 2, 3}})
show_quadratic_equation_solution(a, b, c);
return EXIT_SUCCESS;
}
the way setjmp and longjmp are just passing the baton back and forthβ¦ main character energy honestly π
π¦ Output
so when you actually run it, you get this:
Solving 1xΒ² + -3x + 2 = 0...
xβ = 1, xβ = 2
Solving 2xΒ² + -3x + -2 = 0...
xβ = -0.5, xβ = 2
Solving 1xΒ² + 2x + 3 = 0...
No real solution
first two slayed, third one hit the longjmp and jumped straight to the error handler. no real solution, no problem, we move π
π₯ See also
setjmpβ saves the context (function macro). the little bookmark you drop in the code π·οΈlongjmpβ jumps to specified location (function). the teleport, the fast travel, the "nvm take me back" π- C documentation for
jmp_bufβ the original recipe, if you wanna see where this all came from π
and that's the whole page, bestie!! no cap, you now understand std::jmp_buf better than half the people who use it π
this lil gem was pulled from http://cppreference.com, dated 2024.06.10, and it's page std::jmp_buf(3) of the manual β go forth and jump responsibly, mwah πβ¨π¦
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms