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 saver
  • void 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): setjmp and longjmp make programs hard to understand and maintain. If possible, an alternative should be used. fr fr. also 😭 longjmp can 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. if longjmp() is invoked with a second argument of 0, 1 will be returned instead. no cap.

Parameters

  • __jmpb β€” Information saved by a previous call to setjmp().
  • __ret β€” Value to return to the caller of setjmp().

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 type jmp_buf which 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