jmp_buf goto_label; /* declared globally */ void might_do_nonlocal_goto() { ... if (trouble) longjmp( goto_label, 1 ); ... } void handles_exception() { if (setjmp( goto_label ) == 0) { /* normal code for the program */ ... might_do_nonlocal_goto(); ... } else { /* target of nonlocal goto */ ... } }
Given this, setjmp() must save the return address and stack pointer, plus any other saved regster values that are needed, into the jump buffer. The setjmp function would return zero in an entirely normal way. The longjmp() function would operate by first setting the stack pointer to the value saved in the jump buffer and then loading the return address from the jump buffer into the return address location on the stack. If other values are stored in the jump buffer, they would be loaded into registers or into register save areas on the stack. Finally, the longjmp() function would execute a normal function return.