3 ms·
The func can be implemented like this: float func(int *i, float *f) { *i = 42; return *f; } Since the compiler assumes that the pointers i
by BruiseLee 8y ago
The func can be implemented like this:
float func(int *i, float *f) {
*i = 42;
return *f;
}
Since the compiler assumes that the pointers i and f do not alias, it has a right to generate the code that is equivalent to:
float func(int *i, float *f) {
float result = *f;
*i = 42;
return result;
}
But this will change the result of the function. So in practice if you want to do type punning using unions, the compiler must know about it.
- boomlinde 8y agoSure, by not accessing the members through the union type you lose the information the compiler needs to relax strict aliasing assumptions for union access. It's a potential footgun, but IMO only with careful aiming. It's also not strictly related to union member pointers but applies generally to aliased pointers.