hash table lookup consistenly fails

P

Pieter Claassen

The following code works in a test case, but when I try to run it in a
larger program, it consistently fails. gdm indicates that the problem
comes in with realloc.

I am doing anything obviously stupid?

Thanks in advance.
P

pkt_t *get_state(char *ident){
fprintf(stderr,"ENTER GET STATE \n");
ENTRY e,*t;
pkt_t *packet;
fprintf(stderr,"ID=%s\n",ident);
e.key=ident;
fprintf(stderr,"JUST SET THE KEY\n");
t=hsearch(e,ENTER);
fprintf(stderr,"JUST DONE A SEARCH\n");

if (t == NULL){
fprintf(stderr,"NO ENTRY FOUND\n");
return NULL;
}
fprintf(stderr,"JUST FINISHED THE LOOKUP AND FOUND SOMETHING\n");
//Cast pointer to struct pkt and get to state member
packet=((pkt_t*)t->data);
//Now return it
fprintf(stderr,"EXIT GET STATE \n");
return packet;
}
 
G

Gordon Burditt

The following code works in a test case, but when I try to run it in a
larger program, it consistently fails. gdm indicates that the problem
comes in with realloc.

I am doing anything obviously stupid?

What is gdm? Gnome Display Manager? Or did you perhaps mean GDBM,
which seems to make more sense in this context?


Are you storing a struct (a pkt_t) as the data portion of a record?
Various database packages including *dbm do not guarantee that the
data will be aligned in any particular manner, so if you store
something that needs alignment (typically a double, or a struct,
or int, or whatever), it will probably come back in a data buffer
with pessimal alignment. Therefore, just pointing a struct pointer
at it and trying to use the struct won't work. Usual fix: memcpy()
the data to an appropriate object, THEN use it from that place.

Another issue with *dbm is the lifetime of the data in the buffer.
In general, if you look something up and want it to stay around,
copy it before reading or writing another key/data pair.



Thanks in advance.
P

pkt_t *get_state(char *ident){
fprintf(stderr,"ENTER GET STATE \n");
ENTRY e,*t;
pkt_t *packet;
fprintf(stderr,"ID=%s\n",ident);
e.key=ident;
fprintf(stderr,"JUST SET THE KEY\n");
t=hsearch(e,ENTER);
fprintf(stderr,"JUST DONE A SEARCH\n");

if (t == NULL){
fprintf(stderr,"NO ENTRY FOUND\n");
return NULL;
}
fprintf(stderr,"JUST FINISHED THE LOOKUP AND FOUND SOMETHING\n");
//Cast pointer to struct pkt and get to state member
packet=((pkt_t*)t->data);
//Now return it
fprintf(stderr,"EXIT GET STATE \n");
return packet;
}

Gordon L. Burditt
 
C

CBFalconer

Pieter said:
The following code works in a test case, but when I try to run it
in a larger program, it consistently fails. gdm indicates that the
problem comes in with realloc.

I am doing anything obviously stupid?

It doesn't compile in any compiler known to me. Cut your program
down to a minimum compilable example, not over 200 lines or so,
which shows the problem. Post that with a clear indication of
where and how it fails. Do not use non-standard C. Then possibly
somebody will look at it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top