I am primarily interested in string keys. Can someone point me towards a library?
14 Answers
I had the same need and did some research and ended up using libcfu
It's simple and readable so if I have a need to modify, I can do it without spending too much time to understand. It's also of BSD license. No need to change my structs (to embed say a next pointer)
I had to reject the other options for following reasons (my personal reasons, YMMV):
- sglib --> it's a macro maze and I wasn't comfortable debugging/making changes on such a code base using just macros
- cbfalconer --> lot of licensing redflags, and the site was down and too many unfavorable discussions on web about support/author; didn't want to take the risk
- google sparce-hash --> as stated already, it's for C++, not C
- glib (gnome hash) --> looked very promising; but I couldn't find any easy way to install the developer kit; I just needed the C routines/files -- not the full blown developement environment
- Judy --> seems too complex for a simple use.. also was not ready to debug myself if I had to run into any issues
- npsml (mentioned here) --> can't find the source
- strmap found very simple and useful -- it's just too simplistic that both key and value must be strings; value being string seems too restrictive (should accept void *)
- uthash --> seems good (has been mentioned on wikipedia on hashtable); found that it requires struct to be modified -- didn't want to do that as performace is not really a concern for my use --it's more of development velocity.
In summary for very simple use strmap is good; uthash if you are concerned with additional memory use. If just speed of development or ease of use is primary objective, libcfu wins [note libcfu internally does memory allocation to maintain the nodes/hashtables]. It's surprising that there aren't many simple C hash implementations available.

- 442
- 3
- 8

- 769
- 5
- 3
-
3i notice uthash seems to be more actively developed than libcfu (2005 vintage). perhaps this isn't an issue for a small bit of code though - have you come across any other contenders since this post? – bph Jan 23 '12 at 10:27
-
I have a huge dataset, and glib does not support that big data (32-bit keys). I need more than glib. How about libcfu? – Baskaya Feb 25 '13 at 19:14
-
libcfu link is showing an error... – zippy Jul 07 '14 at 15:06
-
1The hashtable implementation in CPython is based on libcfu: https://github.com/python/cpython/blob/master/Modules/hashtable.c – Ryan Li Jul 02 '15 at 18:04
-
Libcfu 's document: http://libcfu.sourceforge.net/libcfu.html – Zhou Hongbo Jun 02 '22 at 07:49
-
Finally I choose UTHash on Android. Some notes: if occured `fall-through between switch labels`, please use `#pragma GCC diagnostic ignored "-Wimplicit-fallthrough"`; if occured `padding struct 'struct UT_hash_table' with 4 bytes to align 'tail'`, see https://github.com/troydhanson/uthash/issues/118#issue-223582662 – Zhou Hongbo Jun 15 '22 at 09:20
GLib is a great library to use as a foundation in your C projects. They have some decent data structure offerings including Hash Tables: http://developer.gnome.org/glib/2.28/glib-Hash-Tables.html (link updated 4/6/2011)

- 81
- 1
- 2
- 12

- 161
- 3
-
-
1am i right in thinking you usually dynamically link to the glib library to use these data structures, potentially creating porting issues if moving from linux to windows? – bph Jan 23 '12 at 10:35
-
Glib supports only 32-bit. If you work with huge data, glib won't be a good choice – Baskaya Feb 25 '13 at 19:10
For strings, the Judy Array might be good.
A Judy array is a complex but very fast associative array data structure for storing and looking up values using integer or string keys. Unlike normal arrays, Judy arrays may be sparse; that is, they may have large ranges of unassigned indices.
Here is a Judy library in C.
A C library that provides a state-of-the-art core technology that implements a sparse dynamic array. Judy arrays are declared simply with a null pointer. A Judy array consumes memory only when it is populated, yet can grow to take advantage of all available memory if desired.
Other references,
This Wikipedia hash implementation reference has some C
open source links.
Also, cmph -- A Minimal Perfect Hashing Library in C
, supports several algorithms.

- 13,254
- 3
- 41
- 57
There are some good answers here:
Container Class / Library for C
http://sglib.sourceforge.net.
http://cbfalconer.home.att.net/download/

- 1
- 1

- 15,244
- 11
- 66
- 92
Dave Hanson's C Interfaces and Implementations includes a fine hash table and several other well-engineered data structures. There is also a nice string-processing interface. The book is great if you can afford it, but even if not, I have found this software very well designed, small enough to learn in its entirety, and easy to reuse in several different projects.

- 198,648
- 61
- 360
- 533
A long time has passed since I asked this question... I can now add my own public domain library to the list:

- 27,279
- 27
- 74
- 92
C Interfaces and Implementations discusses hash table implementations in C. The source code is available online. (My copy of the book is at work so I can't be more specific.)

- 1,085
- 2
- 13
- 22
Apache's APR library has its own hash-implementation. It is already ported to anything Apache runs on and the Apache license is rather liberal too.

- 3,043
- 3
- 29
- 46
-
This looks to be a really practical choice for C programming. It has everything, widely used and tested, well documented, .. – minghua Sep 14 '13 at 14:23
khash.h from samtools/bwa/seqtk/klib
curl https://raw.github.com/attractivechaos/klib/master/khash.h

- 1,757
- 4
- 21
- 32
-
2although khash looks like it's written to be very efficient, one thing it lacks is documentation/an toy example of it in use... – Andy Hayden Oct 11 '13 at 22:32
-
-
2I see one example, which doesn't offer any explanation and similar to the docs it has one letter non-descriptive variable names and no comments. It's annoying as no doubt it's doing something straightforward. – Andy Hayden Oct 13 '13 at 18:02
https://github.com/dozylynx/C-hashtable
[updated URL as original now 404s: http://www.cl.cam.ac.uk/~cwc22/hashtable/ ]
Defined functions
* create_hashtable
* hashtable_insert
* hashtable_search
* hashtable_remove
* hashtable_count
* hashtable_destroy
Example of use
struct hashtable *h;
struct some_key *k;
struct some_value *v;
static unsigned int hash_from_key_fn( void *k );
static int keys_equal_fn ( void *key1, void *key2 );
h = create_hashtable(16, hash_from_key_fn, keys_equal_fn);
insert_key = (struct some_key *) malloc(sizeof(struct some_key));
retrieve_key = (struct some_key *) malloc(sizeof(struct some_key));
v = (struct some_value *) malloc(sizeof(struct some_value));
(You should initialise insert_key, retrieve_key and v here)
if (! hashtable_insert(h,insert_key,v) )
{ exit(-1); }
if (NULL == (found = hashtable_search(h,retrieve_key) ))
{ printf("not found!"); }
if (NULL == (found = hashtable_remove(h,retrieve_key) ))
{ printf("Not found\n"); }
hashtable_destroy(h,1); /* second arg indicates "free(value)" */

- 406
- 3
- 9

- 7,843
- 1
- 27
- 25
-
7
-
I am the author of the above hash table. The code is available at: https://github.com/dozylynx/C-hashtable – xtopher Nov 21 '19 at 04:31
Gperf - Perfect Hash Function Generator
http://www.ibm.com/developerworks/linux/library/l-gperf.html
stl has map and hash_map (hash_map is only in some implementations) that are key to value if you are able to use C++.

- 7,843
- 1
- 27
- 25