Hard
Type punning through reinterpret_cast
Compiled with -O2 (strict aliasing on, the GCC/Clang default at that level), what does this program print?
main.cpp
#include <cstdio>
float reinterpretAsFloat(int* ip) {
float* fp = reinterpret_cast<float*>(ip);
*ip = 0x3F800000; // bit pattern for 1.0f
return *fp; // read back through an incompatible pointer type
}
int main() {
int x = 0;
printf("%f\n", reinterpretAsFloat(&x));
}2 of 44 in Compilers