Re:You Can't Always Hash Pointers in C
by tehrei on Tuesday May 31, 2016 @10:48AM
(#11806802)
Oh, so then on C++ you can check if pointer-to-T p is within T a[sz]; by doing (a == p || std::less(a, p)) && (std::less(p, a + sz))
[ Parent
| on HN ]
Possibly not. The standard guarantees that less yields a total order for pointers. But the ordering itself is unspecified, and not even guaranteed to be consistent with operator< for pointers to elements of the same array.
I thought it was UB to use < on pointers outside of the same block of memory. e.g. your function is fine if the answer is true, but UB if the answer is false.
I could be misremembering though, but I do know I'd always cast to uintptr_t for this.