Live data from Hacker News

15-line hash table in C

pastes.archbsd.net

31–40 of 104 posts

Re: 15-line hash table in C

#31

There's no bounds testing in hget(): some valid sequences of operations will cause buffer overflows. For example, this should segfault: int (**table)[2] = hnew(); for (int j=0; j The problem is, the probing function doesn't wrap (the "t += h" part), so if you have have several colliding keys, it will probe for them past the end of the table.

Yeah... it’s easy to write small code that doesn’t work.

It's not hard to fix, however - one extra modulus, as far as I can see.

Re: 15-line hash table in C

#32

Is this some common style? int (**hnew()) I've never seen parens used like that. Usually it's: int **hnew()

[] has higher precedence than *, so without the parens you get "function returning array of two pointer to pointer to int" (which is illegal) instead of the desired "function returning pointer to pointer to array of two int".

Re: 15-line hash table in C

#34
post #15

People understand that the compiler/executable doesn't run any faster the less newlines there are, right? This is cute, but as others have pointed out it, it isn't really a correct implementation of a hash table. Also, it wouldn't pass a code review anywhere I've ever worked.

> This is cute

Well that's the whole point, right? This is clearly written for fun, it's not production code, it's not supposed to pass a code review. It's just supposed to be cool and interesting, and a challenge for the author.

Re: 15-line hash table in C

#37
post #15

People understand that the compiler/executable doesn't run any faster the less newlines there are, right? This is cute, but as others have pointed out it, it isn't really a correct implementation of a hash table. Also, it wouldn't pass a code review anywhere I've ever worked.

> This is cute Well that's the whole point, right? This is clearly written for fun, it's not production code, it's not supposed to pass a code review. It's just supposed to be cool and interesting, and a challenge for the author.

Exactly! Building cute but ultimately useless[0] programs is a lot of fun and makes for great brainteasers. It's also a great way to interact in a friendly but still technical way with your programmer peers. I recommend it to everyone!

[0] - http://lelandbatey.com/posts/2014/09/binary-tree-printer/

Re: 15-line hash table in C

#39
post #27

If you rewrote this exact logic in Python it would be longer, because then the code would actually be readable. However, fortunately it's just two chars: {}

It seems to me that the exact logic behind those two chars is roughly 150 times longer :) ( https://github.com/python-git/python/blob/master/Objects/dic... )
Post reply on HN