strict aliasing

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

 



Hello,

I am sorry that I ask about this topic,
even if it has been discussed so often, but I am really at a loss:

First of all is there any website, link or docu (whatever) which lists some
examples of what is legal (under strict aliasing) and what is not ?

Let me try to explain what I understood:

With the following variable definitions,

unsigned int *ptr1;
unsigned short *ptr2;

"strict aliasing" means that the compiler is allowed to assume that
"ptr1" and "ptr2" will NOT acceess to the same address (object).

For optimization this means that if someone writes

ptr2[0]=0x1111
ptr1[0]=0x22222222;

then the compiler can assume that ptr2[0] still is "0x1111", because "ptr1"
must not access to memory which is accessed through ptr2.

Is this correct so far ?



Now I am writing code for an embedded application (no OS).

Because I know the architecture I want to do the following
---
unsigned int value;
unsigned char *ptr2;

*((unsigned int *)ptr2) = value;
---

The idea here is that you write a 32 bit value in big endian order to a
byte (unsigned char) array.

(The used architecture is big endian; I know that on some architectures
that means that the pointer must point to an address which is aligned...)


Now my understanding was that this code breaks strict aliasing rules,
because I cast a "unsigned char *" to an "unsigned int *".

BUT: The g++ 4.4.2 compiler does not complain with "-Wstrict-aliasing".

Is the above statement OK or not ?

Or is it, because "ptr2" is an "unsigned char *", that ptr2 is assumed to
be able to alias anything ? (Because it is a char pointer).

Does these "char pointer can alias anything" really only refer to "char *"
or also to "unsigned char *".

with best regards
  Ingo

[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