Skip to main content
Journey Uncommon Logo
JourneyUncommon
hardC++strict aliasing rulesSingle-choice MCQ

Why is `std::memcpy` the well-defined way to reinterpret a `float`'s bits as a `unsigned`, while `*reinterpret_cast<unsigned*>(&f)` is undefined behavior?

float f = 3.14f; unsigned u; std::memcpy(&u, &f, sizeof u); // well-defined // unsigned u2 = *reinterpret_cast<unsigned*>(&f); // UB