Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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;
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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’

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading