This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

aliasing warnings


Since gcc 4.4 I get considerably more
"dereferencing type-punned pointer will break strict-aliasing rules"
warnings with my code. The code ist used for message en- and de-capsulation.

The basic message structure is defined like that

struct msg {
  uint8_t msg_type;
  uint8_t msg_len;
  uint8_t msg_data[0];
};

Upon reception of the message by means of a socket the space needed for the message is allocated and stored as a pointer to struct msg. Depending on the msg_type there can be different contents in the message.

One example of message content are two 32 bit integers. With gcc 4.3 -Wall and -O3 I could access the contents without a warning:

struct msg *msg = receive();
if (msg->msg_type==5 && msg->msg_len==8) {
  printf("a: %d\n", *(uint32_t*)(msg->msg_data+0));
  printf("b: %d\n", *(uint32_t*)(msg->msg_data+4));
}

Now with gcc 4.4 I get the type-punned pointer warning - but only for the (a) printf, the (b) printf appears to be fine. Regardless of the warning the printf statements always give the correct text on standard output.

Reading several posts about the "strict aliasing rules" in C99 i ask myself what gcc optimization (in future) might break the mentioned code.

Furthermore it is strange that the first printf triggers an aliasing warning and the second printf not. I cannot find a difference with regards to the C99 strict aliasing rules. Could some put any light on this?

Thanks,
Eduard


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]