1

so I don't know what I should do with my unload function to make it work properly? Because valgrind isn't happy at all and it's giving 1000000 errors (literally). The load function works perfectly (I added it for referencing in my code below) but unload is wonky as hell.

Thanks in advance for the help!

Kind Regards
PrimeBeat

// Loads dictionary into memory, returning true if successful, else false
bool load(const char *dictionary)
{
    // load the dictionary file
    FILE *dict_file = fopen(dictionary, "r");
    if (dict_file == NULL)
    {
        return false;
    }

    // temporary storage for each word
    char buffer[LENGTH + 1];

    // read the individual strings from the dictionary file
    while (fscanf(dict_file, "%s", buffer) != EOF)
    {
        // allocate memory for a new node (create a node)
        node *new_node = malloc(sizeof(node));
        if (new_node == NULL)
        {
            return false;
        }

        // copy the word into the newly created node
        strcpy(new_node->word, buffer);
        new_node->next = NULL;

        if (table[hash(buffer)] == NULL)
        {
            table[hash(buffer)] = malloc(sizeof(node));
            table[hash(buffer)] = new_node;
        }
        else
        {
            new_node->next = table[hash(buffer)];
            table[hash(buffer)] = new_node;
        }

        size_of_dict++;
    }

    // free the dictionary file
    fclose(dict_file);
    return true;
}

// Returns number of words in dictionary if loaded, else 0 if not yet loaded
unsigned int size(void)
{
    return size_of_dict;
}

// Unloads dictionary from memory, returning true if successful, else false
bool unload(void)
{
    //loop through the hash table
    for (int i = 0; i < N; i++)
    {
        struct node *current_table = table[i];
        while (current_table != NULL)
        {
            struct node *cursor = table[i];
            struct node *tmp = cursor;
            cursor = cursor->next;
            free(tmp);
        }
        free(current_table);
        free(table[i]);
    }
    return true;
}
1

The load function works perfectly

I'm afraid I'm gonna have to disagree. You're calling malloc twice when the list is empty. So that's one node per list that you can effectively never free, as the pointer to that node is immediately orphaned.

As for the unload function. You problems are limited to this section:

        struct node *current_table = table[i]; // maybe you meant cursor up here?
        while (current_table != NULL)
        {
            struct node *cursor = table[i]; // why is this here? Move up?
            struct node *tmp = cursor;
            cursor = cursor->next;
            free(tmp); // this should be the only call to free. 
        }
        free(current_table); // This is not right
        free(table[i]); // neither is this

The while loop checks if current_table is NULL, but cursor is the pointer being incremented. You don't need both of these.

The last two calls to free don't make sense. I'm guessing this has to do with the extra node you're allocating when starting a new list.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .