Thread: freeing hash table

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    67

    freeing hash table

    i want to free the hash table. does this function look correct?

    Code:
    void free_table (void)
    {
    	int i;
    	struct hash_list *v, *temp;
    	
    	for (i = 0; i < HASHSIZE; i++){
    		v = hashtab[i];
    		while (v != NULL){
    			temp = v;
    			v = v -> next;
    			free (temp -> name);
    			free (temp);
    		}
    	}printf ("Done\n");
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >i want to free the hash table.
    Which hash table? Yours? It's hard to tell you if your code is correct without knowing how you've structured and built the hash table.

    >does this function look correct?
    It looks okay at a glance. Why do you ask?
    My best code is written with the delete key.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    The code formatting could be better, especially the final printf following a brace, should be on a new line.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    67
    Code:
    struct hash_list{ char *name; struct hash_list *next; } hash_list;
    
    #define HASHSIZE 6151
    
    static struct hash_list *hashtab[HASHSIZE];
    
    void init_table(void)
    {
    	unsigned int i;
    	for (i = 0; i < HASHSIZE; i++)
    		hashtab[i] = NULL;
    }
    here is how i built my hash table

  5. #5
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    It looks correct.
    Did you have some errors or were just wondering ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dictionary in C# to hash table in C?
    By dinoman in forum C Programming
    Replies: 2
    Last Post: 04-12-2009, 09:23 PM
  2. Writing array, to file
    By zootreeves in forum C Programming
    Replies: 9
    Last Post: 09-08-2007, 05:06 PM
  3. Group Project Help/Volunteer
    By DarkDot in forum C++ Programming
    Replies: 3
    Last Post: 04-24-2007, 11:36 PM
  4. Hash table creation and insertion
    By tgshah in forum C Programming
    Replies: 1
    Last Post: 01-23-2006, 07:54 PM
  5. Not sure on hash table memory allocations
    By Thumper333 in forum C Programming
    Replies: 3
    Last Post: 09-27-2004, 09:00 PM