setjmp(3avr)
β¨ NAME β¨
setjmp β it's from <setjmp.h>, and it's all about non-local goto bestie π
(this is the setjmp(3avr) page, for the record, bestie, in case you got lost in the man pages again)
πΈ SYNOPSIS πΈ
π Functions π
int setjmp (jmp_buf __jmpb)β the savervoid longjmp (jmp_buf __jmpb, int __ret)β the jumper
π¦ Detailed Description π¦
okay so. the C language already has the goto statement, and yes it has main character energy, but it's lowkey limited: it can only be used to jump to a label in the same (local) function. that's it. that's the whole range.
so if you need to jump directly to another, non-local function? the C library has you covered with setjmp and longjmp. π«Ά these two are genuinely useful for dealing with errors and interrupts that get encountered way down deep in a low-level subroutine of a program. ikykyk.
β οΈ Note (and this is real, not a vibe check):setjmpandlongjmpmake programs hard to understand and maintain. If possible, an alternative should be used. fr fr. also πlongjmpcan destroy changes made to global register variables (see How to permanently bind a variable to a register?).
for a very detailed discussion of setjmp/longjmp, see Chapter 7 of Advanced Programming in the UNIX Environment, by W. Richard Stevens. it's giving required reading π
Example:
#include <setjmp.h>
jmp_buf env;
int main (void)
{
if (setjmp (env))
{
// Handle error ...
}
while (1)
{
// Main processing loop which calls foo() somewhere ...
}
}
void foo (void)
{
// blah, blah, blah ...
if (err)
{
longjmp (env, 1);
}
}
π Function Documentation π
π void longjmp (jmp_buf __jmpb, int __ret) [extern]
non-local jump to a saved stack context. periodt.
#include <setjmp.h>
longjmp() restores the environment saved by the last call of setjmp() with the corresponding __jmpb argument. after longjmp() is completed, program execution continues as if the corresponding call of setjmp() had just returned the value __ret. it literally rewinds time β¨
β οΈ Note:longjmp()cannot cause 0 to be returned. iflongjmp()is invoked with a second argument of0,1will be returned instead. no cap.
Parameters
__jmpbβ Information saved by a previous call tosetjmp().__retβ Value to return to the caller ofsetjmp().
Returns
This function never returns. π iconic behaviour honestly
π int setjmp (jmp_buf __jmpb) [extern]
save stack context for non-local goto.
#include <setjmp.h>
setjmp() saves the stack context/environment in __jmpb for later use by longjmp(). π
β οΈ heads up bestie: the stack context will be invalidated if the function which called setjmp() returns. so like, don't do that.
Parameters
__jmpbβ Variable of typejmp_bufwhich holds the stack information such that the environment can be restored.
Returns
setjmp() returns 0 if returning directly, and non-zero when returning from longjmp() using the saved context. two paths, one function, main character energy π
π Author π
Generated automatically by Doxygen for AVR-LibC from the source code. it didn't even have to try π
and that's the whole page, bestie!! π¦ this was the setjmp page from AVR-LibC, Version 2.2.1, sliced off the setjmp(3avr) vibes β go forth and jump non-locally (responsibly), mwah πβ¨
π¦ where this page comes from
- ubuntu resolute: avr-libc 1:2.2.1-1build1 Β· BSD-3-Clause
- ubuntu stonking: avr-libc 1:2.2.1-1build1 Β· BSD-3-Clause
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms