volatile semantics
Ian Lance Taylor
ian@airs.com
Sun Jul 17 08:25:00 GMT 2005
Daniel Berlin <dberlin@dberlin.org> writes:
> > In other words, we're asked to agree that the type of an object
> > changes depending on how it is accessed.
> > For the benefit of readers, only the first sentence of this para is
> > the language of the standard; the rest isn't.
> >
> > That an object referred to through a volatile pointer must
> > "temporarily" be treated as though it were declared volatile is the
> > crux of this argument.
>
> Again, you could say the same about const, restrict, or any other
> qualifier then, making them more or less useless as qualifiers.
I think there may be a difference.
const is inherently a characteristic of the object. It applies at
definition time. Casting away const in a reference does not change
the definition. Whether making an assignment through a pointer after
casting away const is legal depends upon how the definition of the
object pointed to is handled (C99 6.7.3: "If an attempt is made to
modify an object defined with a const-qualified type through use of an
lvalue with non-const-qualified type, the behavior is undefined").
restrict is less interesting, since it simply provides a guarantee
about a pointer. Casting away restrict does nothing useful--it just
abandons the guarantee. Casting to restrict is by definition only
valid if the guarantee holds--if the object is indeed restricted (C99
6.7.3.1: ".... If these requirements are not met, then the behavior is
undefined").
volatile, on the other hand, is inherently a characteristic of the
access, not of the object. Defining a volatile object does nothing in
itself; it merely affects all accesses to the object. Thus casting to
volatile should mean that all uses of the resulting pointer should be
done in a volatile fashion--i.e., all reads and writes done precisely
as specified by the standard's "abstract machine." Casting away from
volatile means the reverse--there are no restrictions on the use of
the object to which the pointer points.
I am not aware of any other type qualifiers in C or C++.
Ian
More information about the Gcc
mailing list