The general answer when asking "how does one implement memcpy function conformant with strict aliasing rules" is something along the lines of
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, compiler is free to reorder call to memcpy and access to the dest, because it can reorder writes to char* with reads from any other pointer type (strict aliasing rules prevent only reordering of reads from char* with writes to any other pointer type).
Is this correct and if yes, are there any ways to correctly implement memcpy, or should we just rely on builtin memcpy?
Please note, that this question concerns not only memcpy but any deserialization/decoding function.
memcpy
as a built-in function and do the right thing. As for how it works in standard C, you implement it with character types, as you mentioned. Anything else will be implementation-specific. – YlSomeData *dest, *src; memcpy(dest, src); dest->...
– Potful