News for hackers. Stuff that matters.
Re:You Can't Always Hash Pointers in C by gpderetta on Tuesday May 31, 2016 @06:46AM (#11805782)
This remind me of a story.

When Stephanov was implementing the STL, he needed a strict weak ordering among all pointers to implement things like std::map.

Instead of wasting time arguing within the committee on wether operator< should have the required semantics, he simply added std::less as a primitive and default comparison for std::map. It is specified to call operator< for every T, except that for pointer it relies on unspecified compiler support for doing the right thing.

On pretty much all implementations it simply calls operator< even for pointers.

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))
Re:You Can't Always Hash Pointers in C by gpderetta on Tuesday May 31, 2016 @04:50PM (#11810104)
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.
Re:You Can't Always Hash Pointers in C by yoklov on Wednesday June 01, 2016 @11:39AM (#11815263)
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.