4 ms·
Is a cast to uintptr_t also OK ? void *oldptr = malloc(73); uintptr_t savedptr = (uintptr_t)oldptr; void *newptr = realloc(oldptr, 42); if (savedptr ==
by amadvance 4y ago
Is a cast to uintptr_t also OK ?
void *oldptr = malloc(73);
uintptr_t savedptr = (uintptr_t)oldptr;
void *newptr = realloc(oldptr, 42);
if (savedptr == (uintptr_t)newptr) {
// not changed
}
- munch117 4y agoThat is OK, at least in the sense that there's no undefined behaviour. I'm not sure it's also OK, because the memcmp suggestion you are replying to seems a bit suspect. There's still the problem that comparing the uintptr_t's is not guaranteed to yield the same result as the pointers they were cast from. But that's merely implementation-specific behaviour, not undefined.
- kazinator 4y agoI think it's fine on the assumption that a uintptr_t converted from a pointer cannot be a trap representation, which is the case on mainstream platforms. (An unsigned type can only have trap patterns if it has padding bits. Every combination of the value bits is a valid value according to the pure binary encoding.)