A line with *ap = is a strict aliens violation: an object of type B written using the lvalue expression of type A
Suppose there is no line, and we switched to ap->x = 10; ap->y = 20; ap->x = 10; ap->y = 20; . In this case, an int type lvalue is used for the int object type.
There is disagreement that this is a strict violation of pseudonyms or not. I think the Standard letter says that itโs not, but others (including the gcc and clang developers) see ap->x as implying that *ap . Most agree that the standard definition of strict aliases is too vague and needs to be improved.
Sample code using structure definitions:
void f(A* ap, B* bp) { ap->x = 213; ++bp->x; ap->x = 213; ++bp->x; } int main() { B b = { 0 }; f( (A *)&b, &b ); printf("%d\n", bx); }
For me, this produces 214 in -O2 and 2 in -O3 , with gcc. The generated build on godbolt for gcc 6.3 was:
f: movl (%rsi), %eax movl $213, (%rdi) addl $2, %eax movl %eax, (%rsi) ret
which shows that the compiler has changed this function to:
int temp = bp->x + 2; ap->x = 213; bp->x = temp;
and therefore, the compiler must consider that ap->x cannot be an alias of bp->x .
MM
source share