ron.e...@gmail.com
unread,Jul 7, 2016, 8:25:34 AM7/7/16Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to uthash
up vote
0
down vote
favorite
I would like to utilize uthash for some hash tables in my system but I need all memory to be pre-allocated statically. How can I modify uthash to work with pre-allocated memory? I now have the following (which uses dynamic allocation with malloc())
[code]
#include "uthash.h"
#include <stdio.h>
struct my_struct {
int label; /* we'll use this field as the key */
int bfd;
UT_hash_handle hh; /* makes this structure hashable */
};
struct my_struct *users = NULL;
void add_user(int label, int bfd) {
struct my_struct *s;
s = malloc(sizeof(struct my_struct));
s->label = label;
s->bfd = bfd;
HASH_ADD_INT( users, label, s ); /* id: name of key field */
}
struct my_struct *find_user(int label) {
static struct my_struct *s;
HASH_FIND_INT( users, &label, s ); /* s: output pointer */
return s;
}
int main (void)
{
static struct my_struct *res = NULL;
add_user(123456,1);
add_user(12345,2);
add_user(1234567,3);
res = find_user(12345);
printf("12345 %d\n",res->bfd);
res = find_user(1234567);
printf("1234567 %d\n",res->bfd);
res = find_user(123456);
printf("123456 %d\n",res->bfd);
return 0;
}
[/code]