🔸Error Recovery
setjmp / longjmp
using these 2 we can jump out of deeply nested call chain without needing to deal with handling errors in every function in the chain.
setjmp saves a copy of program counter and current pointer to the top of the stack
used the variable j to remember where you are now must be called first.
longjmp is then invoked after setjmp, longjmp(jmp_buf j, int i) saysgor back to the place that the j is remembering restores the process in the state that it existed when it called setjmp returns the value of i so the coe can tell when we got back here via longjmp() the constents of the j are distroyed when it is used in a longjmp()
this is often reffered to as unwinding the stack because we unroll activation records from the stack until we get to the saved one.
<setjmp.h> should be included
we can only longjmp back to somewhere we have already been, where we did setjmp and that still has a live activation record.
jumping from a loop:
jumping between functions:
example:
Last updated