[uthash] HASH_FIND_INT deletes Items?

45 views
Skip to first unread message

alexand...@googlemail.com

unread,
Jun 21, 2018, 1:18:11 PM6/21/18
to uthash
Hi guys I got a question.
I am trying to use uthash.h. I wrote a simple code like in the tutorial. I added a Key,value pair and after that try to find it via HASH_FIND_INT. The first time HASH_FIND_INT finds my entry but the second time it fails.
Can someone explain this behaviour to me?

My Code looks like this: (LOG_INFO just prints to my console)

#include "uthash.h"

struct my_struct {
int id;
float value;
UT_hash_handle hh;
};


struct my_struct *users = NULL;


static void add_hash(int user_id, float value)
{
struct my_struct *s;
HASH_FIND_INT(users, &user_id, s); /* id already in the hash? */
if (s==NULL)
{
s = (struct my_struct *)malloc(sizeof *s);
s->id = user_id;
s->value = value;
HASH_ADD_INT( users, id, s ); /* id: name of key field */
}
else
{
s->value = value;
}
}

int main(void) {
int key_one = 1;

struct my_struct *s;
char name[8]={0};
float value_f;

add_hash(key_one,1.5);
LOG_INFO(Log_Stdout, "Key_one added");

HASH_FIND_INT(users,&key_one,s);
if(s==NULL)
{
LOG_INFO(Log_Stdout, "key_one is NULL");
}
else
{
value_f = s->value;
sprintf(name, "key_one s value = %f", value_f);
LOG_INFO(Log_Stdout, name);
}


HASH_FIND_INT(users,&key_one,s);
if(s==NULL)
{
LOG_INFO(Log_Stdout, "key_one is NULL");
}
else
{
value_f = s->value;
sprintf(name, "key_one s value = %f", value_f);
LOG_INFO(Log_Stdout, name);
}
return 0;
}


and my console looks like this:

[2018-06-21 12:15:14.530 (UTC+0200)] Key_one added
[2018-06-21 12:15:14.536 (UTC+0200)] key_one s value = 1.500000
[2018-06-21 12:15:14.542 (UTC+0200)] key_one is NULL
Reply all
Reply to author
Forward
0 new messages