3 ms·
The only local variables you can count on being preserved are "volatile" variables initialised before the setjmp() call, and then not changed. That's all the g
by Findecanor 4y ago
The only local variables you can count on being preserved are "volatile" variables initialised before the setjmp() call, and then not changed.
That's all the guarantee the C standard gives. Anything else is specific to compiler/OS.
- adrian_b 4y agoAs you have phrased them, the standard guarantees seem weaker than they are actually specified in the C standard. Where a "longjmp" returns after a "setjmp", everything is preserved exactly like after a normal function call (including the registers specified by the ABI), "except that the values of objects of automatic storage duration that are local to the function containing the invocation of the corresponding setjmp macro that do not have volatile-qualified type and have been changed between the setjmp invocation and longjmp call are indeterminate." For the most frequent use case, when "longjmp" is used to throw exceptions, the only local variables that can become indeterminate, according to the standard, are those that have been passed as arguments to functions invoked inside the C equivalent of a "try" block, or which have been explicitly modified in another way there. Any local variable that is not assigned to or passed as an argument there is preserved. The standard behavior is normal, because where a "longjmp" returns it is not known where the execution of the nested functions has been aborted and whether any of their output parameters already store their expected final values or only some unpredictable temporary values.