Consider the following code:
*((unsigned int*)((unsigned char *)0x400000));
Does it violate strict aliasing?
From the strict aliasing rule:
An object shall have its stored value accessed only by an lvalue expression that has one of the following types:
...
For a violation to occur, there must be an object of type unsigned char
, so when accessed with unsigned int*
a violation will occur.
Now, does (unsigned char *)0x400000
constitute an unsigned char
object at address 0x400000
? if not, than there is actually no object with stored value of type unsigned char
here, so the access to it with a unsigned int
does not violate the rule.
Note the following phrase about object:
Object
region of data storage in the execution environment, the contents of which can represent values
NOTE When referenced, an object may be interpreted as having a particular type; see 6.3.2.1.
So, since (unsigned char *)0x400000
does not constitute an unsigned char object reference (to my understanding) there is no object of type unsigned char
in the presented code, so it seems that there is no violation.
Am I correct?
With respect to @Antti Haapala answer:
If we assume that integer to pointer conversion of both converting 0x400000
to unsigned char*
and to unsigned int*
has on my system a defined behavior with no trap representation and well aligned, and that is in order to fill the implementation-defined gap from the standard:
An integer may be converted to any pointer type. Except as previously specified, the result is implementation-defined, might not be correctly aligned, might not point to an entity of the referenced type, and might be a trap representation
Will that change the answer to my question?