0

I am so close to finally finishing Speller, however I'm running into an issue with Valgrind that I don't understand.

I was under the impression that when I malloc a node for the dictionary table in load, they are all freed in the unload function that's called at the end of the program, but I am clearly wrong...

Code below:

// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
    FILE *loaded_dictionary = fopen(dictionary, "r");
    // read strings from doc one at a time
    char current_word[LENGTH+1];

    while(fscanf(loaded_dictionary, "%s", current_word) != EOF)
    {
        node *n = malloc(sizeof(node));
        if(n == NULL) {
            return false;
        }

        dictionary_word_count++;

        int hash_table_location = hash(current_word);

        strcpy(n->word, current_word);
        n->next = NULL;

        if(table[hash_table_location] == NULL)
        {
            table[hash_table_location] = n;
        }
        else
        {
            node *tmp = table[hash_table_location];

            if(tmp == NULL)
            {
                *tmp = *n;
            }
            else
            {
                while(tmp->next != NULL)
                {
                    tmp = tmp->next;
                }
                tmp->next = n;
            }
        }
    }
    fclose(loaded_dictionary);
    return true;
}

// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
    for (int i = 0; i <= N; i++)
    {
        for (node* j = 0; j != NULL; j++)
        {
            node* cursor = table[i];
            node* tmp = cursor;

            cursor = cursor->next;
            free(tmp);
        }
    }
    return true;
}

// Staff ~cs50/2019/fall/pset5/speller dictionaries/small texts/cat.txt
// mine ./speller dictionaries/small texts/cat.txt

And Valgrind output:

==8142== HEAP SUMMARY:
==8142==     in use at exit: 448 bytes in 8 blocks
==8142==   total heap usage: 13 allocs, 5 frees, 10,768 bytes allocated
==8142== 
==8142== 448 bytes in 8 blocks are still reachable in loss record 1 of 1
==8142==    at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==8142==    by 0x4011C1: load (dictionary.c:84)
==8142==    by 0x400984: main (speller.c:40)
==8142== 
==8142== LEAK SUMMARY:
==8142==    definitely lost: 0 bytes in 0 blocks
==8142==    indirectly lost: 0 bytes in 0 blocks
==8142==      possibly lost: 0 bytes in 0 blocks
==8142==    still reachable: 448 bytes in 8 blocks
==8142==         suppressed: 0 bytes in 0 blocks

Line 84 is node *n = malloc(sizeof(node));, however as I mentioned, I believed each node was freed up in unload. Can anyone point me to what I might be missing?

Cheers.

1 Answer 1

1

Does unload free anything? To see for yourself, set a breakpoint at the free in the function, and run the program with debug50. The j loop exits immediately because j is always != NULL.

1
  • It doesn't! Thanks so much - I fixed unload and we're green 👍
    – Hinchy
    Commented Jun 23, 2020 at 4:05

You must log in to answer this question.

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