fooling strict aliasing?
Andrew Haley
aph@redhat.com
Wed Sep 29 18:09:00 GMT 2010
On 09/29/2010 07:39 AM, André Bjärby wrote:
> The following cobe generates a warning "dereferencing type-punned
> pointer will break strict-aliasing rules" in function foo(). When I
> add an intermediate variable (as in function bar()) the warning
> disappears.
>
> #include <stdint.h>
>
> typedef struct blob_St {
> uint32_t length;
> char data[0] __attribute__ ((aligned (sizeof (uint32_t))));;
> } blob_t;
>
>
> int
> foo (blob_t *arg)
> {
> return *(uint32_t*)arg->data == 10;
> }
>
> int
> bar (blob_t *arg)
> {
> uint32_t *tmp;
>
> tmp = (uint32_t*)arg->data;
> return *tmp == 10;
> }
>
>
> This code was compiled using 'gcc -c -Wall test.c -O2'. With gcc
> version 4.4 I get the error, with versions 4.3 and 4.1 I don't.
>
> My questions are:
> 1. Why does the error disappear when I use an intermediate variable?
Because gcc doesn't do full data-flow analysis.
> 2. Do I run the risk of generating incorrent code if using an
> intermediate variable and compiling with strict aliasing?
Yes.
Andrew.
More information about the Gcc-help
mailing list