4 ms·
Interesting. What if I use memcpy and memcmp? As in, void foo(void *a, void *b) { char ca[sizeof(void*)]; char cb[sizeof(void*)]; memmove
by tehrei 10y ago
Interesting. What if I use memcpy and memcmp? As in,
void foo(void *a, void *b) {
char ca[sizeof(void*)];
char cb[sizeof(void*)];
memmove(ca, &a, sizeof(void*));
memmove(cb, &b, sizeof(void*));
assert((a == b) == (memcmp(ca, cb, sizeof(void*)) == 0));
}
- mikeash 10y agoThat doesn't help. Your assert can be false. Consider, for example, a 32-bit architecture which ignores the top 8 bits of the pointer (as was the case in the old 68000 Macs, as discussed elsewhere in these comments). Consider two pointers which differ only in a top bit, like 0x1000f000 and 0x0000f000. They will compare equal, but the underlying bytes extracted with your memmove call will differ. And this isn't just a historical curiosity. ARM64 can optionally ignore the top eight bits of its 64-bit pointers, for example.