by bluetomcat · 10y ago · 0 comments
The casts to uintptr_t are not needed in this case, as the result of subtracting two pointers already yields ptrdiff_t.
Also, the mere subtraction of two pointers that do not point to the same "array object" yields undefined behaviour as per the standard.
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;~1 beat~
Yikes!
~leaves self-shaped dust cloud behind as hurriedly runs toward higher [level] ground~