Ingo Rohloff wrote: > 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 ? Yes. Any version of the C standard will tell you. http://www.open-std.org/JTC1/SC22/WG14/www/standards > 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). Right. > 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 ? Yes. > 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. That's perfectly OK. Standard C has special rules for character types, which alias everything. > (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 *". No, because of the special rules for character types. > 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). That's right. > Does these "char pointer can alias anything" really only refer to "char *" > or also to "unsigned char *". All character types. But don't believe me: go to http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1124.pdf and read 6.3.2.3 Paragraph 7. Andrew.