Get the latest tech news
How to implement a hash table in C (2021)
An explanation of how to implement a simple hash table data structure, with code and examples in the C programming language.
Recently I wrote an article that compared a simple program that counts word frequencies across various languages, and one of the things that came up was how C doesn’t have a hash table data structure in its standard library. I got some good feedback from Code Review Stack Exchange that helped clean up a few sharp edges, not the least of which was a memory leak due to how I was calling strdup during the ht_expand step (fixed here). However, a quick, non-scientific performance comparison with Go’s map implementation shows that it compares pretty well – with half a million English words, this C version is about 50% slower for lookups and 40% faster for insertion.
Or read this on Hacker News