Anything related to the setjmp() and longjmp() routines in the C standard library (provided by the setjmp.h header file), which provide control flow that modify the usual call and return sequence of a subroutine.
The setjmp
and longjmp
pair of functions allow for the creation of non-local jumping in C programs—it is essentially a non-local goto
that can jump across functions. This is a feature which is basically equivalent to exceptions in later languages, and has actually been used to write exception-like systems for C.
It works by loading the environmental state (jmp_buf
) saved by an earlier setjmp
over the current state. In the process, it also returns a value at the site of the original setjmp
call. In practice, the environmental state is tied in with the stack, and this can cause some trouble.
For example, if the frame in which setjmp
returns, then jumping to that particular frame invokes undefined behavior because that frame no longer exists. Another consequence of longjmp
is that no stack unwinding occurs, so open files will not be closed and heap variables will not be freed.
In most C standard libraries, it can be found under setjmp.h
. It has the call signature:
void longjmp(jmp_buf env, int val);
If you do not understand any of the above, then please do not use longjmp
! It is very much a tool that can bite you if you are inexperienced. This is especially true for C++, which has exceptions which are harder to abuse than longjmp
is.