3 ms·
The code is still wrong for another reason: if (uintptr_t)ptrOld < (uintptr_t)ptrNew, then the subtraction is calculated modulo some power of 2 yielding a large
by sltkr 11y ago
The code is still wrong for another reason: if (uintptr_t)ptrOld < (uintptr_t)ptrNew, then the subtraction is calculated modulo some power of 2 yielding a large number. The result of converting this to a signed integral type like ptrdiff_t is implementation defined.
In practice it works only if sizeof(ptrdiff_t) == sizeof(uintptr_t) AND the system uses two's complement for signed integers. Neither property is guaranteed by the standard.
I think the correct way to find the byte offset is this:
ptrdiff_t diff = (char*)ptrOld - (char*)ptrNew;
- _kst_ 11y agoAnd it's wrong for yet another reason. You can convert a pointer to uintprt_t, but arithmetic on the result isn't necessarily meaningful.
- shawn-furyan 11y agoThis thread makes me eager to get back into the minimal C programming that I left behind in my college assembly class. ~1 beat~ Yikes! ~leaves self-shaped dust cloud behind as hurriedly runs toward higher [level] ground~