Non-Local Jumps
Local
jumps, or jumps within a function, are
performed by the goto
statement. The macro
setjmp(), on the other hand, marks a location in
the program (by storing the pertinent process information) so that
execution can be resumed at that point at any time by a call to the
function longjmp(). The
longjmp() function and the
setjmp() macro are declared in the header file
setjmp.h
.
-
intsetjmp( jmp_bufenv); Saves the current calling environment (CPU registers and stack) in the buffer
env, which has the typejmp_buf.-
voidlongjmp( jmp_bufenv, intretval); Restores the saved environment, so that program execution continues at the point where
setjmp()was called.
The program can use the return value of setjmp()
to determine whether setjmp() itself was just
called, or whether a jump to this point by means of
longjmp() has just occurred.
setjmp() itself returns the value
0, but after a call to
longjmp() the apparent return value of
setjmp() is the value of the argument
retval. If
retval is equal to 0,
the apparent return value is 1.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access