P
Peter Pichler
A colleague encountered an interesting problem. Suppose we have a C
function like this:
void WRITE_THING(void* addr, THING t)
{
memcpy(addr, &t, sizeof t);
}
to copy a THING to any byte address (which might not be aligned for a
THING, hence not doing *(THING*)addr = t).
There's a platform on which it fails, i.e. after the function the
contents of addr are not the thing written to it. (addr is an address in
a malloc'd area, hence properly aligned for THING).
Using our own function like this:
void MemCopy(void* dest, void* src, size_t sz)
{
unsigned char* t = dest;
unsigned char* s = src;
while (sz--)
*t++ = *s++;
}
DOES work. To us, the system memcpy looks broken. Any other opinions on
why the memcpy version wouldn't work?
Thanks for any ideas,
Peter
function like this:
void WRITE_THING(void* addr, THING t)
{
memcpy(addr, &t, sizeof t);
}
to copy a THING to any byte address (which might not be aligned for a
THING, hence not doing *(THING*)addr = t).
There's a platform on which it fails, i.e. after the function the
contents of addr are not the thing written to it. (addr is an address in
a malloc'd area, hence properly aligned for THING).
Using our own function like this:
void MemCopy(void* dest, void* src, size_t sz)
{
unsigned char* t = dest;
unsigned char* s = src;
while (sz--)
*t++ = *s++;
}
DOES work. To us, the system memcpy looks broken. Any other opinions on
why the memcpy version wouldn't work?
Thanks for any ideas,
Peter