3 ms·
> This leads to a problem when e.g. taking i8 elements [3;7) and reinterpreting as an i32 array. Even ignoring alignment issues, this is already UB because it
by tylerhou 3y ago
> This leads to a problem when e.g. taking i8 elements [3;7) and reinterpreting as an i32 array.
Even ignoring alignment issues, this is already UB because it violates the strict aliasing rule. You technically need to memcpy and hope that the compiler optimizes the memcpy out. In C++20 you can use std::bit_cast in some circumstances. https://en.cppreference.com/w/cpp/numeric/bit_cast https://en.cppreference.com/w/cpp/numeric/bit_cast. In C11 you can use a union, but that still requires a "copy" into the union.
- dzaima 3y agoI'm of course already using -fno-strict-aliasing (primarily because without it it's impossible to implement a custom memory allocator, but it also helps here).