My questions aims at the behaviour of setjmp/longjmp concerning local variables.
Example code:
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 documentation of setjmp/longjmp says:
"All accessible objects have values as of the time longjmp() was called, except that the values of objects of automatic storage duration which are local to the function containing the invocation of the corresponding setjmp() which do not have volatile-qualified type and which are changed between the setjmp() invocation and longjmp() call are indeterminate."
I see following two possible interpretations:
intepretation1:
Local variables are restored, except those that are both
- non-volatile and
- changed
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 are defined. According to interpretation2 after longjmp only v4 is defined. v1, v2, v3 are undefined.
Which one is right?
BTW: I need a general ("portable") answer that is valid for all compilers, i.e. trying out with one particular compiler doesn't help.