I want to create a hashmap with uthash.
I want the key and the value to be a struct, containing a String and a size_t like this:
typedef struct hash_ptr {
char* string;
size_t len;
}hash_ptr;
The hash table itself looks like this:
typedef struct hash_map_entry {
struct hash_ptr *key;
struct hash_ptr *value;
UT_hash_handle hh;
}hash_map_entry;
In order to add new entries to the map I wrote a new function called add_entry():
void add_entry(hash_map_entry *map, hash_ptr *key, hash_ptr *value) {
hash_map_entry *entry;
HASH_FIND(hh, map, key, sizeof *key, entry);
if (entry == NULL) {
entry = (hash_map_entry*) malloc(sizeof *entry);
memset(entry, 0, sizeof *entry);
entry->value = value;
entry->key = key;
HASH_ADD(hh, map, key, sizeof *key, entry);
}
}
However, after initializing and calling add_entry()...
hash_map_entry *map = NULL;
hash_ptr *key = (hash_ptr*) malloc(sizeof *key);
memset(key, 0, sizeof *key);
key->string = "Is this the Krusty Krab?";
key->len = strlen(key->string);
hash_ptr *value = (hash_ptr*) malloc(sizeof *value);
memset(value, 0, sizeof *value);
value->string = "No, this is Patrick!";
value->len = strlen(value->string);
add_entry(map, key, value);
...HASH_FIND doesn't find the added entry:
hash_map_entry *find_me;
HASH_FIND(hh, map, key, sizeof *key, find_me);
and find_me is NULL.
I followed the Instructions for using a struct as a key from the official user guide.
Where am I wrong?