Advertisement
Intermediate C++, Casting & Conversions

What is reinterpret_cast and why is it dangerous?

Short answer

reinterpret_cast reinterprets the bit pattern of one type as another (e.g., pointer to integer, or between unrelated pointer types).

Save C++ mock interview
Advertisement
AModel answer
reinterpret_cast reinterprets the bit pattern of one type as another (e.g., pointer to integer, or between unrelated pointer types). It performs no conversion of value, only a compile-time reinterpretation, and misusing it easily leads to undefined behavior (alignment violations, strict-aliasing breaks). It's for low-level code (hardware, serialization) and should be used rarely and carefully.
auto addr = reinterpret_cast<std::uintptr_t>(ptr); // pointer to int
Real-world example
Low-level driver code uses reinterpret_cast to view a device register address as an integer.
Likely follow-ups
  • What does it actually change? | Why can it cause UB?
Related topics