Points To Catch
- As for the control flow:
setjmpreturns twice, andlongjmpnever returns. - When you call
setjmpfor the first time, to store the environment, it returns zero, - And then when you call
longjmp, the control flow passes to return fromsetjmpwith 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
setjmpbefore the correspondinglongjmp. In other words,longjmpmust only be called abovesetjmpin 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: