ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am writing a program that uses a hash table in C.
I have been using hcreate and hsearch encountering in some weird behavior.
In the man pages it says that when I add an entry inthe hash table using hseearch, a copy of the item is made and put in the hash table. I found this to be not true since if I free the memory of the item that I just added to the memory, also the item in the table is lost. This means that there is no copy in the hash table but only a pointer pointing to the structure I allocated.
Another problem is that I used hcreate(10000) and even though I have only 10 entries in the table I get collisions (different keys give me the same data) and the keys are unique since each key is a MAC address.
Anyone can help in anyway? Do you know of any other hash table implementation/API in C?
#include <stdio.h>
#include <search.h>
#include <string.h>
struct info
{ /* this is the info stored in the table */
int age;
int room; /* other than the key. */
};
#define NUM_EMPL 5000 /* # of elements in search table */
int main(int argc, char *argv[] )
{
/* space to store strings employee name 19 char */
char string_space[NUM_EMPL*20];
/* space to store employee info */
struct info info_space[NUM_EMPL];
/* next avail space in string_space */
char *str_ptr = string_space;
/* next avail space in info_space */
struct info *info_ptr = info_space;
ENTRY item, *found_item, *hsearch( );
/* name to look for in table */
char name_to_find[30];
int i = 0;
/* create table */
(void) hcreate(NUM_EMPL);
while (scanf("%s%d%d", str_ptr, &info_ptr->age,
&info_ptr->room) != EOF && i++ < NUM_EMPL) {
/* put info in structure, and structure in item */
item.key = str_ptr;
item.data = (char *)info_ptr;
str_ptr += strlen(str_ptr) + 1;
info_ptr++;
/* put item into table */
(void) hsearch(item, ENTER);
}
/* access table */
item.key = name_to_find;
while (scanf("%s", item.key) != EOF) {
if ((found_item = hsearch(item, FIND)) != NULL) {
/* if item is in the table */
printf("found %s, age = %d, room = %d\n",
found_item->key,
((struct info *)found_item->data)->age,
((struct info *)found_item->data)->room);
} else {
(void)printf("no such employee %s\n",
name_to_find);
}
}
return 0;
}
Last edited by jim mcnamara; 06-03-2005 at 04:59 PM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.