Why does this code break strict-aliasing rules?

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

 



Hi, everyone!

I have some code which is giving strict-aliasing warnings on gcc 4.4.1,
and I don't understand why.

When I compile alias-warning2.c (attached), I get

$ gcc -O2 -Wall alias-warning2.c -o alias-warning2
alias-warning2.c: In function 'main':
alias-warning2.c:31: warning: dereferencing pointer 'd2.16' does break
strict-aliasing rules
alias-warning2.c:39: note: initialized from here

and indeed, when I run, gcc has optimized out the offending code:

$ ./alias-warning2
Aliasing is unhappy!  d2.base.a == 5

However, as far as I can tell, at line 31 the object d2.base (which has
type 'struct base') is indeed being accessed through a pointer of type
'struct base*'.

If I change the inline function to take the type 'struct base*'
directly, rather than 'union derived_union*', I do not get a warning, or
mis-compilaton.

Am I missing something, or is gcc mis-compiling correct code?


$ gcc -v
Using built-in specs.
Target: i486-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu
4.4.1-4ubuntu8' --with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs
--enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr
--enable-shared --enable-multiarch --enable-linker-build-id
--with-system-zlib --libexecdir=/usr/lib --without-included-gettext
--enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.4
--program-suffix=-4.4 --enable-nls --enable-clocale=gnu
--enable-libstdcxx-debug --enable-objc-gc --enable-targets=all
--disable-werror --with-arch-32=i486 --with-tune=generic
--enable-checking=release --build=i486-linux-gnu --host=i486-linux-gnu
--target=i486-linux-gnu
Thread model: posix
gcc version 4.4.1 (Ubuntu 4.4.1-4ubuntu8)

-- 
Jonathan Lennox
Vidyo, Inc
jonathan@xxxxxxxxx

Attachment: alias-warning2.c
Description: alias-warning2.c


[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