setjmp: read the manual
dmr at research.UUCP
dmr at research.UUCP
Thu Oct 4 15:03:04 AEST 1984
V7, 4.[01]BSD, and SVr0 manuals include this sentence in the setjmp/longjmp
documentation (setjmp(3): "All accessible data have values as of the time
longjmp was called." So setjmp shouldn't save register variables; they're
irrelevant. That is,
...
register x = 1;
if (setjmp(sj_buf)) {
printf("x = %d\n", x)
exit(0);
}
x = 2;
f();
}
f() { longjmp(sj_buf, 1); }
is supposed to print "x = 2" and longjmp is supposed to trace back in the
stack to make sure this happens. (This is nontrivial, because the
place where the register that contained x was saved can be anywhere in
call chain.)
There is a very good reason for doing it this way: it makes
register and automatic variables behave the same. The specification
could have said that data values are restored to those at
the time that setjmp was called, but it doesn't (and shouldn't:
that's a lot of stuff to save).
I checked the behavior on V8, 4.2BSD, and SVr2. It works right
(prints 2) on V8 and 4.2, works wrong (prints 1) on SV (on VAX, 3B20, 3B2).
I don't have 4.2 or SVr2 manuals at hand so I don't know what they say.
Dennis Ritchie
More information about the Comp.lang.c
mailing list