Thread: Question on free() ing a hash table

  1. #1
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329

    Thumbs up Question on free() ing a hash table

    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)
    {
     int h;
     struct nlist *prev, *np;
    
     prev = NULL;
     h = hash(s);
     for(np = hashtab[h]; np!=NULL; np = np->next){
           if(strcmp(s, np->name) == 0)
                break;
           prev = np;
     }
    
     if(np !=NULL) {
       if(prev == NULL)
          hashtab[h] = np->next;
       else
          prev->next = np->next;
      free((void *) np->name);
      free((void *) np->defn);
      free((void *) np);
     }
    }
    The question is, how come I have to free() both np->name and np->defn? Ie, how come I can't just free() np?

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    The question is, how come I have to free() both np->name and np->defn? Ie, how come I can't just free() np?
    I assume our data structure would look some thing like this.
    Code:
    struct nlist 
    {
           char *name;
           char *defn;
    };
    Once your create the node and allocated memory for name and defn, these two pointer are now pointing at the newly allocated memory location. And I belive you have should be allocating somewhere in the code. Have a look at your. Hence in order to free these allocated memory as well you free name and defn as well. Just freeing np would normally lead up to memory leak, which not you wanted.

    If you had something like this
    Code:
    struct nlist 
    {
           char name[10];
           char defn[10];
    };
    Then you could have just freed np.

    ssharish

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    The void* casts in your calls to free are pointless.
    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
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Overworked_PhD View Post
    The question is, how come I have to free() both np->name and np->defn? Ie, how come I can't just free() np?
    All you have to do is look at the code that does the allocation. Every malloc must be matched here with a free, and so if it mallocs name and defn, then you have to free them here. If it doesn't, then you don't.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Duplicate at Hash Table
    By DarrenY in forum C Programming
    Replies: 1
    Last Post: 05-10-2007, 02:31 AM
  2. Insert Items in Hash Table Container.
    By joenching in forum C++ Programming
    Replies: 18
    Last Post: 05-02-2005, 01:50 PM
  3. inputting words from a file
    By kashifk in forum C++ Programming
    Replies: 5
    Last Post: 10-24-2003, 07:18 AM
  4. rehash help
    By kashifk in forum C++ Programming
    Replies: 1
    Last Post: 10-22-2003, 06:51 PM
  5. help with operator <
    By kashifk in forum C++ Programming
    Replies: 1
    Last Post: 10-21-2003, 03:49 PM