strict-aliasing question

Ian Lance Taylor iant@google.com
Tue Sep 8 23:58:00 GMT 2009


Jörg Richter <Joerg.Richter@pdv-FS.de> writes:

> I thought I have understood the aliasing-rules. But what is wrong with this:
>
> $ cat alias.cc
> #include <memory>
>
> struct Col
> {
>     short val;
> };
>
> void func()
> {
>   char arr[sizeof(Col)] __attribute__ ((aligned(__alignof__(Col))));
>   new (arr) Col;
>   ((Col*)arr)->~Col();          // line 12
> }
>
> $ /tools/pkg/gcc/4.4.1/bin/g++ -o alias.o -c alias.cc -Wall -O2
> alias.cc: In function void func():
> alias.cc:12: warning: dereferencing type-punned pointer will break
> strict-aliasing rules

The type char[] is not compatible with the type Col*.

You might think they are because of the special exception for char*, but
char[] is not char*.

Ian



More information about the Gcc-help mailing list