My questions are about the behavior of setjmp / longjmp regarding local variables.
Code example:
jmp_buf env; void abc() { int error; ... if(error) longjmp(env); } void xyz() { int v1; // non-volatile; changed between setjmp and longjmp int v2; // non-volatile; not changed between setjmp and longjmp volatile int v3; // volatile; changed between setjmp and longjmp volatile int v4; // volatile; not changed between setjmp and longjmp ... if(setjmp(env)) { // error handling ... return; } v1++; // change v1 v3++; // change v3 abc(); } int main(...) { xyz(); }
The setjmp / longjmp documentation says:
"All available objects have values ββsince the call to longjmp () except that the values ββof the objects of automatic storage time that are local to the function containing the call to the corresponding setjmp (), which are not of a variable type and which are changed between the call to setjmp () and longjmp () are undefined. "
I see the following two possible interpretations:
intepretation1:
Local variables are restored, except for those that are
intepretation2:
Local variables are restored except
- those that are non-volatile and
- those that are changed
According to interpretation1 after longjmp only v1 is undefined. v2, v3, v4. According to interpretation2, only v4 is defined after longjmp. v1, v2, v3 undefined.
Which one is right?
BTW: I need a generic ("portable") answer that is valid for all compilers, i.e. Trying with one specific compiler does not help.
c
Curd Sep 08 '09 at 11:21 2009-09-08 11:21
source share