This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Union an alias when the difference is the volatile qualifier?
- From: Andrew Haley <aph at redhat dot com>
- To: noloader at gmail dot com, "gcc-help at gcc dot gnu dot org" <gcc-help at gcc dot gnu dot org>
- Date: Sun, 02 Aug 2015 15:27:33 +0100
- Subject: Re: Union an alias when the difference is the volatile qualifier?
- Authentication-results: sourceware.org; auth=none
- References: <CAH8yC8memwTgcYFZuABvg+wt1fEavdouey5wmE6YkPNqdVb00Q at mail dot gmail dot com>
On 02/08/15 12:14, Jeffrey Walton wrote:
> According to -fno-strict-aliasing in the online manual
> (https://gcc.gnu.org/onlinedocs/gcc-4.4.2/gcc/Optimize-Options.html):
>
> Allow the compiler to assume the strictest aliasing rules ...
> ... unless the types are almost the same.
>
> I think "almost the same" applies to sizes and alignments, and not
> qualifiers like const and volatile.
>
> Does GCC consider an 'int' and a 'volatile int' (or 'int*' and a
> 'volatile int*') almost the same?
It's not symmetrical.
The rules are the rules of the C standard. I don't think there's any
intention to use GCC-specific rules. So:
<<
ISO/IEC 9899:1999 (E)
6.2.7 Compatible type and composite type
1 Two types have compatible type if their types are the
same. Additional rules for determining whether two types are
compatible are described in 6.7.2 for type specifiers, in 6.7.3 for
type qualifiers, and in 6.7.5 for declarators.
... For two qualified types to be compatible, both shall have the
identically qualified version of a compatible type;
6.7.3 Type qualifiers
Syntax 1 type-qualifier: const restrict volatile
Semantics 3 4
If an attempt is made to refer to an object defined with a
volatile-qualified type through use of an lvalue with
non-volatile-qualified type, the behavior is undefined.
>>
It's OK to cast a pointer to int to a pointer to volatile int and then
dereference that pointer, but not vice versa. So, GCC must assume
that a pointer to volatile int may point to a non-volatile int object.
But the reverse is not true: GCC need not assume that a pointer to int
may point to a volatile int.
Andrew.