LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-03-2005, 11:20 AM   #1
ajpug
LQ Newbie
 
Registered: Sep 2003
Posts: 12

Rep: Reputation: 0
libc hcreate and hsearch.


Hi all,

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?

Thank you,
ajpug
 
Old 06-03-2005, 04:55 PM   #2
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
Here is a separate example that works.
Code:
#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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
libc.so.6(GLIBC_2.3), libc.so.6(GLIBC_2.3.2) needed for Linux 7.3.1 vivek_thampy Linux - Software 1 08-09-2004 04:12 PM
libc linuxanswer Programming 7 11-17-2003 11:15 AM
libc.so.6 alperen Linux - Software 1 09-15-2003 11:46 AM
libc.so verse libc.so.5 markstevens Linux - Software 4 06-19-2003 11:41 AM
libc.so.6 bash Linux - Distributions 2 08-09-2002 07:39 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration