The example works fine, until I add code to check for duplicates. The output is then "Segmentation fault (core dumped)"
I'm figuring it must be a rookie mistake. Can anybody spot where I've gone wrong? I've commented my changes below
#include <string.h> /* strcpy */
#include <stdlib.h> /* malloc */
#include <stdio.h> /* printf */
#include <uthash.h>
struct my_struct {
char name[10]; /* key (string is WITHIN the structure) */
int id;
UT_hash_handle hh; /* makes this structure hashable */
};
int main(int argc, char *argv[]) {
// modified this line to add duplicates
const char *names[] = { "joe", "bob", "betty", "johnny", "betty", "johnny", "bob", "joe", NULL };
struct my_struct *s, *tmp, *users = NULL;
for (int i = 0; names[i]; ++i) {
s = (struct my_struct *)malloc(sizeof *s);
strcpy(s->name, names[i]);
s->id = i;
// added the lines below to check for duplicates
HASH_FIND_STR( users, names[i], s);
if (s) {
printf("Danger, duplicate detected: %s\n", names[i]);
} else {
HASH_ADD_STR( users, name, s );
}
}
HASH_FIND_STR( users, "bob", s);
if (s) printf("%s's id is %d\n", s->name, s->id);
for(s=users; s != NULL; s=s->hh.next) {
printf("user id %d: name %s\n", s->id, s->name);
}
/* free the hash table contents */
HASH_ITER(hh, users, s, tmp) {
HASH_DEL(users, s);
free(s);
}
return 0;
}