fooling strict aliasing?

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

 



Hi,
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?
2. Do I run the risk of generating incorrent code if using an
intermediate variable and compiling with strict aliasing?

Thank you.

/A


[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