The portability issue with reinterpret_cast<>
lies in the fact that different CPUs store numbers differently in memory. Some store them from the least significant byte up to the most significant one (little endian), others do it precisely the other way around (big endian). Some even use some weird byte order like 1 0 3 2
, don't ask me why.
Anyway, the consequence of this is, that reinterpret_cast<>
is portable as long as you do not rely on the byte order in any way.
Your example code does not rely on byte order, it treats all bytes the same (setting them to zero), so that code is portable. If you would use a reinterpret_cast<>
to copy some data object on the same machine without interpreting the bytes, the code would also be portable (memcpy()
does this).
What is not portable is stuff like taking a look at the first byte to determine the sign of a number (works only on big endian machines). If you try to transfer data from one machine to another by just sending the result of reinterpret_cast<char*>
, you are also in trouble: the target machine may use a different byte order than the source machine, completely misinterpreting your data.
I would say that it is wrong to say that the reinterpret_cast<>
is non-portable, it simply exposes a machine detail to the C++ code which is machine-specific. And any code that relies on that machine detail is non-portable.