News for hackers. Stuff that matters.
Re:You Can't Always Hash Pointers in C (2016) by delhanty on Tuesday September 18, 2018 @11:22PM (#18021440)
Generally, hashing pointers in C or C++ is a bad idea anyway.

Unless, you have a particular love for debugging non-reproducible behavior that is.

Re:You Can't Always Hash Pointers in C (2016) by delhanty on Wednesday September 19, 2018 @02:24AM (#18022161)
Clarification: I though this was well know, but apparently it isn't, so I'll explain it.

`malloc` doesn't have to give you the same pointer the next time the program is run - and sometimes it doesn't.

In that case, the ordering of the hash table on iteration can change.

Particularly, release build `malloc` and debug `malloc` are likely to differ, leading to bugs in production that don't reproduce when debugging.

Re:You Can't Always Hash Pointers in C (2016) by wutbrodo on Wednesday September 19, 2018 @02:54AM (#18022293)
> In that case, the ordering of the hash table on iteration can change.

Order-dependent iteration of a hash table's items isn't exactly a central use case of the data structure and often isn't even guaranteed. I don't think that this example is really sufficient to suggest that hashing pointers is a bad idea (though it may be for other reasons).

Re:You Can't Always Hash Pointers in C (2016) by TrinaryWorksToo on Wednesday September 19, 2018 @11:25AM (#18024843)
In golang it is purposefully randomized on every execution to prevent you from relying on iteration order
Re:You Can't Always Hash Pointers in C (2016) by delhanty on Wednesday September 19, 2018 @08:48PM (#18028862)
That's actually better than the situation in C.

In C, debug `malloc` may very well behave the same run after run.

Then you move into production, and release `malloc` behaves differently to debug `malloc`.

Suddenly a latent bug that was there all along shows itself in release, but fails to reproduce in debug.

With the randomized behavior you describe in golang, the bug probably will show in debug and be caught.

Re:You Can't Always Hash Pointers in C (2016) by wutbrodo on Thursday September 20, 2018 @04:50PM (#18035347)
Yea I wasn't sure enough to make a stronger claim, but I don't believe I've ever used ordered iteration of a hashtable. I feel like if I was tempted to do so, it would prompt me to rethink my design. Call it an "algorithm smell"
Re:You Can't Always Hash Pointers in C (2016) by quotemstr on Wednesday September 19, 2018 @02:52AM (#18022279)
Who said you need or want run-to-run hash stability? One common technique for defeating hash collision attacks is maxing a secret random tweak, newly computed on each program run, into to hash function. (Python is one environment that uses this trick.) Under this scheme, hash values are unstable, but the world keeps working. Hash stability turns out not to be particularly important most of the time.

Generally speaking, if your program depends on the stability of hash-table enumeration order, you're doing something wrong in the first place.

Re:You Can't Always Hash Pointers in C (2016) by delhanty on Wednesday September 19, 2018 @05:32AM (#18022980)
>if your program depends on the stability of hash-table enumeration order, you're doing something wrong in the first place

The point is not all programs are correct.

If you're trying to reproduce a bug that occurs in the release version, but goes away in the debug version (because hash-table enumeration order changes) that makes life more difficult.

Re:You Can't Always Hash Pointers in C (2016) by saagarjha on Wednesday September 19, 2018 @01:57AM (#18022042)
I don’t see anything wrong with using pointers as keys in a lookup table. There’s nothing non-reproducible about this.
Re:You Can't Always Hash Pointers in C (2016) by maljx on Wednesday September 19, 2018 @04:22AM (#18022706)
Compiler/runtime can create two pointers with different integer values that point to same memory address. The article explains a few reasons why that might happen. It's very uncommon, but it's valid in the standard.
Re:You Can't Always Hash Pointers in C (2016) by delhanty on Wednesday September 19, 2018 @02:10AM (#18022093)
>There’s nothing non-reproducible about this.

No that's false. See my clarification above.

Re:You Can't Always Hash Pointers in C (2016) by saagarjha on Wednesday September 19, 2018 @03:11AM (#18022362)
I really don't care what the actual value of the pointers are, since I'm not going to use them for anything other than key lookup. The thing that breaks in your case is hash table iteration, but if I'm not doing that I still don't see any non-reproducible behavior other than slight timing differences.
Re:You Can't Always Hash Pointers in C (2016) by Zanni on Wednesday September 19, 2018 @05:11AM (#18022902)
If you read the article, there's an explicit mention of the possibility of tagged pointers for some implementations, where the low-order bits represent the address and the high-order bits are used for housekeeping and might change dynamically. Thus the pointer to the same object might naively cast to different integers at different times in program execution. That will lead to non-reproducible behavior.
Re:You Can't Always Hash Pointers in C (2016) by wruza on Wednesday September 19, 2018 @08:34AM (#18023703)
Given the factual amount of times it will happen, even this small thread is not worth it except for few pedants. And on architectures where it will, and on compilers that dare to make it happen — it will break much more than just your clever code anyway. All these ancient standard details are there only for history. In reality, you cannot even change the direction of memcpy(3) without turning the world upside down.
Re:You Can't Always Hash Pointers in C (2016) by rurban on Wednesday September 19, 2018 @06:56PM (#18028306)
sure, but in practice everybody knows the actually used bits of every pointers per platform. on amd64 e.g. it's the 2 lowest and 16 highest which need to be masked off for comparison. it's simply platform and vm dependent.

I know very well where and how I'm able to compare ptrs in a hash table, and I'm doing it all the time. just complaining out loud is no solution.

Re:You Can't Always Hash Pointers in C (2016) by xamuel on Wednesday September 19, 2018 @02:48AM (#18022262)
There's nothing non-reproducible about it within a particular run. Of course it would be silly to write a pointer hash table to disk!
Re:You Can't Always Hash Pointers in C (2016) by dingo_bat on Wednesday September 19, 2018 @12:26AM (#18021665)
Agreed. Even in the author's use-case, just hashing the string itself would be much better.
Re:You Can't Always Hash Pointers in C (2016) by quotemstr on Wednesday September 19, 2018 @01:22AM (#18021883)
What? No. Hashing a string involves extra pointer chasing and looping over a variable-length data structure, and that's on top of the collision-chain resolution the hash table itself has to do. In performance-sensitive code (like a dynamic language interpreter), it's far better to intern your strings and look them up by ID (i.e., pointer value) directly.