Union struct undefined behaviour in C++?



  • @Gąska said in Union struct undefined behaviour in C++?:

    @dfdub said in Union struct undefined behaviour in C++?:

    Don't ask me why int* and unsigned int* alias (I'd like to know myself!), but according to the standard, they do.

    And according to compilers they don't. Go figure :mlp_shrug:

    Sorry, but that's BS. Look at a suitable function taking an int* and an unsigned int* compiled using -O3 in Compiler Explorer and you'll see that the compiler very much assumes that those might point to the same memory.

    You'll just need a reinterpret_cast to actually perform the pointer conversion. The resulting pointer is safe to use because the type aliasing rules explicitly allow it.


Log in to reply