Eamorr Eamorr - 1 month ago 8
C Question

Implement a hash table

I'm trying to create an efficient look-up table in C.

I have an integer as a key and a variable length

char*
as the value.

I've looked at
uthash
, but this requires a fixed length
char*
value. If I make this a big number, then I'm using too much memory.

struct my_struct {
int key;
char value[10];
UT_hash_handle hh;
};


Has anyone got any pointers? Any insight greatly appreciated.




Thanks everyone for the answers. I've gone with
uthash
and defined my own custom struct to accommodate my data.

Answer

Declare the value field as void *value.

This way you can have any type of data as the value, but the responsibility for allocating and freeing it will be delegated to the client code.

Comments