This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Weird strict-aliasing break
- From: Thibault Castel <thibault dot castel at gmail dot com>
- To: gcc-help at gcc dot gnu dot org
- Date: Mon, 30 May 2011 10:34:40 +0200
- Subject: Weird strict-aliasing break
Hi,
I have a weird warning when compiling my program with g++:
"dereferencing pointer âNAMEâ does break strict-aliasing rules"
So, I tried to investigate a little bit and find some clues : I do a
memory mapping from a char buffer to a C struct to have access to
different datas into the buffer. Pretty casual. But, when I try to
access one of my inner field, the warning triggers... Huh...
To understand a bit more, I wrote a litte test program outside of my
project (really huge). See it in attachment. And I don't really
understand, because the warning is triggered only line 77, not on line
55. Only when I try to access the inner fields of a mapped structured
included into another structure.
I successfully avoid the warning with "__attribute__((__may_alias__))"
but this "cheat" bother me because :
- I don't want to fake the compiler
- I need to compile my huge project both under Linux (gcc/g++) and
Windows (Visual IDE)
- This warning is triggered a hundred times in my huge project (old C
project slowly turning into C++ :/)
I already saw some messages on this list about this warning. But the
difference here is my program IS working. pFoo->a and pFoo->b have
correct values.
I tried different scenarios
- grow or reduce the raw array
- change the definition order into TData
My gcc specs :
=======================
Using built-in specs.
Target: i486-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Debian
4.4.5-8' --with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs
--enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr
--program-suffix=-4.4 --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 --libdir=/usr/lib
--enable-nls --enable-clocale=gnu --enable-libstdcxx-debug
--enable-objc-gc --enable-targets=all --with-arch-32=i586
--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.5 (Debian 4.4.5-8)
=========================
Thanks a lot for any clue or explanation
PS: I'm french, and don't really speak english. I already apologize for that :/
Thibault
#include <stdlib.h>
#include <stdio.h>
typedef struct
{
unsigned char a;
unsigned char b;
unsigned char c;
} TDummy;
// } __attribute__((__may_alias__)) TDummy;
typedef struct
{
unsigned char foo;
unsigned char bar;
char raw[10];
unsigned char tmp;
unsigned char pop;
} TData;
int main (int ac, char** av)
{
TData data;
char raw[10];
TDummy* pFoo = NULL;
char* pChar = NULL;
// Init
for (unsigned char i = 0; i < 10; ++i)
{
data.raw[i] = i;
raw[i] = i;
}
// RAW ACCESS =================================================
printf ("Raw => ");
for (unsigned char i = 0; i < 10; ++i)
{
printf ("%d", raw[i]);
if (i != 9)
printf (",");
}
printf ("\n");
// No warning
pChar = raw;
printf ("Void => ");
printf ("%d,", *(pChar));
pChar++;
printf ("%d\n", *(pChar));
pChar++;
// No warning - Why ???
pFoo = (TDummy*) (raw);
printf ("Dummy => %d,%d\n", pFoo->a, pFoo->b);
// DATA ACCESS =================================================
printf ("Data => ");
for (unsigned char i = 0; i < 10; ++i)
{
printf ("%d", data.raw[i]);
if (i != 9)
printf (",");
}
printf ("\n");
// No warning
pChar = data.raw;
printf ("Void => ");
printf ("%d,", *(pChar));
pChar++;
printf ("%d\n", *(pChar));
pChar++;
// Strict aliasing warning - Why ???
pFoo = (TDummy*) (data.raw);
printf ("Dummy => %d,%d\n", pFoo->a, pFoo->b);
}