Home > Net >  unload() of a hash table doens't seem to work
unload() of a hash table doens't seem to work

Time:04-18

The function below is supposed to unload my hash table node* table[26];. It returns true if the hash table is successfully unloaded, false if it didn't. But here, it keeps on returning false. I don't what I did wrong .

bool unload(void)
{
    // TODO
    for (int i = 0; i < 26; i  )
    {
        free_table(table[i]);
        if (table[i] != NULL)
        {
            return false;
        }
    }
    return true;
}


void free_table(node* hash)
{
    if (hash == NULL)
    {
        return;
    }
    free_table(hash -> next);
    free(hash);
    hash = NULL;
}

CodePudding user response:

In This code

    free_table(table[i]);
    if (table[i] != NULL)

table[i] is not updated by free_table

If you want to update it you must do

   void free_table(node** hash);
   ...
   free_table(&table[i]);
   if (table[i] != NULL)
   ....
   void free_table(node** hash)
   {
      if (*hash == NULL)
      {
         return;
      }
      free_table((*hash) -> next);
      free(*hash);
      *hash = NULL;
}

ie - c style 'pass by reference' , aka 'double pointer'

  • Related