The general response to the query "how to implement the memcpy function that complies with strict alias rules" is something like strings
void *memcpy(void *dest, const void *src, size_t n) { for (size_t i = 0; i < n; i++) ((char*)dest)[i] = ((const char*)src)[i]; return dest; }
However, if I understand correctly, the compiler can freely reorder the memcpy call and access dest, because it can reorder char * entries when reading from any other type of pointer (strict pseudo-decreasing rules only prevent reordering reads from char * with writes to any another type of pointer).
Is this correct, and if so, are there ways to implement memcpy correctly, or should we just rely on the built-in memcpy?
Please note that this question concerns not only memcpy, but also deserialization / decoding functions.
c ++ c strict-aliasing memcpy
Oleg Andreev
source share