Thread: trying to make a hash table......trouble w/ memory?

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    16

    trying to make a hash table......trouble w/ memory?

    For now I am just trying to implement a hash table and a simplified version to see it work. Each entry has a key and a value and a pointer (in case of collisions it is a linked list)
    Then the hash table is an array of those entries. I started initializing the table and printing it out just to make sure things are working. I don't think I'm using malloc correctly for array of structures because I keep getting segmentation fault core dump as an error.
    help

    Code:
    #define TABLE_SIZE ((int) 10)
    struct entryhash
    {
       int key; //order of nodes will be string later to match other stuff
       double num;  //entry is another structure, replace with double for now?
       struct entryhash * next;  //linked structure in case of collision
    };
    
    typedef struct entryhash hash_item;
    
    void print_table (hash_item **head,int tablelen){
      int i;
      hash_item *curr;
      for (i=0; i <tablelen; i++){
          curr=head[i];
          printf("%d\n", curr->key);
          //printf("%lf\n", curr->num);
       }
    }
    
    int main(){
       hash_item ** newhash;
       newhash = (hash_item **)malloc(sizeof(hash_item*) * TABLE_SIZE);
       //initialize everything to null?
       int i;
       for (i=0; i<TABLE_SIZE; i++){
          newhash[i]->key= -1;
          newhash[i]->next=NULL;
       }
       print_table(newhash, TABLE_SIZE);
       //add entries 
       //add_hash_el( &newhash, 9,2.5);
       return 0;
    }

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Allocate storage for each of the hash_item objects that the array of pointers newhash points to before initializing them.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    16
    do you mean going through a loop and assigning
    newhash[i] = (hash_item *)malloc(sizeof(hash_item));
    ?

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Yep! at the top the the loop where the members of hash_item are being initialized to null.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can I make a hash table for my sequences?
    By advancedk in forum C Programming
    Replies: 6
    Last Post: 08-01-2008, 12:25 PM
  2. Group Project Help/Volunteer
    By DarkDot in forum C++ Programming
    Replies: 3
    Last Post: 04-24-2007, 11:36 PM
  3. [Memory management] Hash table?
    By cboard_member in forum Game Programming
    Replies: 9
    Last Post: 04-15-2007, 01:52 PM
  4. Calculating the upper bounds on a hash table.
    By cdalten in forum C Programming
    Replies: 1
    Last Post: 05-01-2006, 06:54 PM
  5. Hash table creation and insertion
    By tgshah in forum C Programming
    Replies: 1
    Last Post: 01-23-2006, 07:54 PM