Points To Catch
- As for the control flow:
setjmp
returns twice, andlongjmp
never returns. - When you call
setjmp
for the first time, to store the environment, it returns zero, - And then when you call
longjmp
, the control flow passes to return fromsetjmp
with the value provided in the argument. - Use cases are generally cited as “error handling”, and “don’t use these functions”.
Note: setjmp
needn’t actually be functions; it may well be a macro. longjmp
is a function, though.
Here’s a little control flow example:
Example
|
|
Important Notes:
- You cannot pass 0 to
longjmp
. If you do, 1 is returned bysetjmp
. - You must not return from the function that called
setjmp
before the correspondinglongjmp
. In other words,longjmp
must only be called abovesetjmp
in the call stack. - You cannot actually store the result of
setjmp
. If you want to return in several different ways, you can use a switch, though: