Live data from Hacker News

15-line hash table in C

pastes.archbsd.net

81–90 of 104 posts

Re: 15-line hash table in C

#81

(Insert my usual rant about not checking malloc()/calloc() for failure.)

Note that on many systems (e.g. Linux), malloc/calloc won't always return NULL when you're out of memory because of lazy memory allocation policies. It will only crash when you start reading / writing. That makes it arguably less useful to test the return value.

edit: clarity.

Re: 15-line hash table in C

#82
post #64

Earlier quoted context omitted.

With two additional casts you can make it compile[1] as C++ - without the incredibly wasteful extra local variable. [1] http://codepad.org/

Your link to " http://codepad.org/" doesn't show any code. Did you forget some arguments in the URL?

http://codepad.org/LuQpUhmj

Re: 15-line hash table in C

#83

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".

So would that be equal to this?

    typedef int arr2int[2];
    typedef arr2int** ptr_arr2int;

    #define SIZE 1024
    static ptr_arr2int hnew() {
        return calloc(sizeof(int**), SIZE);
    }

Re: 15-line hash table in C

#84

What's with the hardcoded size though...

Generally it's easier if the height of a hash table is a fixed value (which can be calculated once at runtime based on how much memory the table can consume) so that the hashing logic is:

target_row_number = table_height % int_value_of_key

If table height keeps changing then this formula would be inconsistent. This code isn't using % but kind of doing the same using &

Re: 15-line hash table in C

#87
post #75

I can see two legitimate cases for writing very short, very dense code. 1: Implement an algorithm in a very concise and straightforward, even if not very efficient, way. An example is the classic quicksort in Haskell, which most literally implements the idea of the algorithm: qsort [] = [] qsort (p:xs) = qsort [ y | y = p ] 2: Implement an algorithm in a super-efficient, while non-obvious, way. An example is the inve…

There is something funny about stating that most "literally" implements the idea of the algorithm, when the main "idea" of the algorithm is to be quick. :)

Also, even in examples of super efficient ways, be wary. The inverse square root you are referring to is actually slower than what many CPUs can do with a single instruction nowdays.

Also, I think you are missing out on the main reason this code was written. Essentially a puzzle to see if it can be done.

Re: 15-line hash table in C

#90
post #71

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.

Every time a lookup is performed, isn't it linearly looking through the table to find that key... That doesn't sound like a hash! Maybe am missing something here.

h seems to be the hash.

    h = k & (SIZE - 1)
Post reply on HN