The portability problem with reinterpret_cast<> is that different CPUs store numbers differently in memory. Some store them from the least significant byte to the most significant (small end), others do it exactly the opposite (big endian). Some even use some kind of weird byte order, like 1 0 3 2 , don't ask me why.
In any case, the consequence of this is that reinterpret_cast<> carried over unless you rely on the byte order in any way.
Your sample code does not rely on the byte order; it processes all bytes in the same way (setting them to zero), so the code is carried over. If you used reinterpret_cast<> to copy some data object on one computer without interpreting bytes, the code would also be portable ( memcpy() does this).
What is not portable is things, for example, look at the first byte to determine the sign of the number (works only on large computers). If you try to transfer data from one computer to another just by sending the result reinterpret_cast<char*> , you also have problems: the target machine may use a different byte order than the original one, completely interpreting your data incorrectly.
I would say that it is wrong to say that reinterpret_cast<> not portable, it just provides the machine details to the C ++ code, which is machine code. And any code that relies on this machine part is not portable.
source share