if long long a[3] = {3, 65535, 65536} and short int *q = (short*) a
why (*(q+9)-q[0]) == -2 is true?
should be UB, Assuming sizeof(short int) == 2 and sizeof(long) == 8) you might have an short int array that looks like this short int b[] = { 3, 0, 0, 0, -1, 0, 0, 0, 0, 1, 0, 0 }; doing b[9] - b[0] yields -2
that is undefined behavior
Assuming if the sizeof long is always 8 and the sizeof short is always 2 and the endianess is always little-endian in such an environment, passing -fno-strict-aliasing flag will make it well-defined behavior.
the language does not define -fno-strict-aliasing, hence it is still UB according to the standard.
Yeah, it is actually a compiler feature to deactivate strict aliasing.
Обсуждают сегодня