473,473 Members | 1,873 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

hash table lookup consistenly fails

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;
}
Nov 14 '05 #1
2 1054
>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
Nov 14 '05 #2
Pieter Claassen wrote:

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.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

Nov 14 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: VK | last post by:
A while ago I proposed to update info in the group FAQ section, but I dropped the discussion using the approach "No matter what color the cat is as long as it still hounts the mice". Over the last...
12
by: wxs | last post by:
Many times we have a bunch of enums we have from either different enums or the same enum that will have various numeric values assigned. Rarely will there be collisions in numbering between the...
2
by: Ravi | last post by:
Hi, I am working on a winform app. I need to use an object which can store some information(key/value pairs) and also can be acessed by multiple threads(read/write). From what I heard Hash table...
1
by: bhups | last post by:
hi, i am doing a lookup using hash table. I have a pipe delimited file that i have to search for a new invocie number field by checking of an old invoice number-customer number key combination...
5
by: Mathias Panzenboeck | last post by:
Hi. I wrote a small hashlib for C. Because I'm new to hashes I looked at pythons implementation and reused *some* of the code... or more the mathematical "hash-function", not really the code. ...
11
by: Douglas Dude | last post by:
how much time does it take to lok for a value - key in a hash_map ? Thanks
139
by: ravi | last post by:
Hi can anybody tell me that which ds will be best suited to implement a hash table in C/C++ thanx. in advanced
1
by: jacob navia | last post by:
Everybody knows that hash tables are fast. What is less known is that perfect hash tables are even faster. A perfect hash table has a hash function and a table layout that avoids collisions...
13
by: Chad | last post by:
Excercise 6-5 Write a functon undef that will remove a name and defintion from the table maintained by lookup and install. Code: unsigned hash(char *s); void undef(char *s)
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.