Re: Union an alias when the difference is the volatile qualifier?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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.




[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux