Strict aliasing rule in C++: what is it and how to avoid UB?
Hello everyone! I started learning C++ and came across the term "strict aliasing rule". Honestly, I don't fully understand what it is and why it's important.\n\nI wrote a small piece of code that, in my opinion, should work, but sometimes behaves strangely. I'm trying to read the contents of a float variable as an array of bytes:\n\n```cpp\n#include \n\nfloat value = 3.14f;\nint* ptr = reinterpret_cast(&value);\nstd::cout << *ptr << std::endl; // I get some garbage value\n```\n\nI thought that `reinterpret_cast` simply reinterprets the bits, and I would get the integer representation of the floating-point number. But the compiler seems to sometimes do something unexpected, and the code may crash or produce strange results.\n\nI heard that this is related to compiler optimizations. My c
No answers yet.
Log in to leave an answer