Volatile is Necessary

Chris Torek chris at mimsy.UUCP
Thu Mar 31 11:57:17 AEST 1988


In article <1259 at PT.CS.CMU.EDU> jgm at K.GP.CS.CMU.EDU (John Myers) writes:
>Is R. Stallman's reading of the ANSI draft wrong, or isn't it
>necessary to declare automatic variables as 'volatile' in order to
>guarantee that their values will be preseved across a setjmp/longjmp?

[\S => section symbol; |word| => C-style text)

p. 119, \S 4.6.2.1, The |longjmp| function

    All accessible objects have values as of the time |longjmp| was
 called, except that the values of objects of automatic storage
 duration that do not have |volatile| type and have been changed
 between the |setjmp| invocation and |longjmp| call are indeterminate.

    As it bypasses the usual function call and return mechanisms,
 ...  However, if the |longjmp| function is invoked from a nested
 signal handler (that is, from a function invoked as the result of
 a signal raised during the handling of another signal), the behavior
 is undefined.  [This answers another recent question.]

On p. 118 (\S 4.6.1.1, The |setjmp| macro), however, we see that

    The |setjmp| macro shall be implemented as a macro, not as an
 actual function.  If the macro definition is suppressed in order
 to access an actual function, the behavior is undefined.

No reason is given here for this requirement (which, incidentally,
makes it technically illegal NOT to |#define setjmp| as something, even
if it is just as |_setjmp|), but it would make sense if it were
intended as a constraint on the *programmer* so that |<setjmp.h>| could
say, e.g.,

	#define setjmp(env) _builtin_setjmp(env)

which would allow the compiler to implement this as a builtin, and
thus to `fix' all local automatic variables, so that the constraint
in \S 4.6.2.1 need not apply.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris at mimsy.umd.edu	Path:	uunet!mimsy!chris



More information about the Comp.lang.c mailing list