0

I've been on the speller problem for a while and I don't understand where i'm going wrong. Any help would be much appreciated. I'm getting the following error from valgrind when i run it alongside --log (the --log CL is used because if i just run valgrind, i am not shown any errors yet my code doesn't pass the check50);

==1460== HEAP SUMMARY:
==1460==     in use at exit: 280 bytes in 5 blocks
==1460==   total heap usage: 11 allocs, 6 frees, 6,564 bytes allocated
==1460== 
==1460== 280 bytes in 5 blocks are still reachable in loss record 1 of 1
==1460==    at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==1460==    by 0x401226: load (dictionary.c:110)
==1460==    by 0x400964: main (speller.c:40)
==1460== 
==1460== LEAK SUMMARY:
==1460==    definitely lost: 0 bytes in 0 blocks
==1460==    indirectly lost: 0 bytes in 0 blocks
==1460==      possibly lost: 0 bytes in 0 blocks
==1460==    still reachable: 280 bytes in 5 blocks
==1460==         suppressed: 0 bytes in 0 blocks
==1460== 
==1460== For counts of detected and suppressed errors, rerun with: -v
==1460== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

Also, my unload funtion is given below

// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
    // loop through hash table
    for (int i = 0; i < N; i++)
    {
        // No linked list at that address
        if (table[i] == NULL)
        {
            break;
        }
        else
        {
            // Traverse node
            node *n = table[i];

            while (n->next != NULL)
            {
                //temporary node for freeing
                node *tmp = n;
                n = tmp->next;
                free(tmp);
            }

            free(n);
            table[i] = NULL; //optional

        }

    }

    free(wordCount);
    return true;
}

Thank you.

1

First, valgrind shows where the memory that is lost was allocated, not necessarily where the problem lies. ;-)

Look at the following code:

for (int i = 0; i < N; i++)
{
    // No linked list at that address
    if (table[i] == NULL)
    {
        break;
    }

What happens when a break statement is encountered inside a for or while loop? It breaks out of the loop. That's what happens here. It's breaking out of the loop and terminating the unload function immediately. The "correct" code would have been a continue statement instead of a break. However, there's a better way....

bool unload(void)
{
    // loop through hash table
    for (int i = 0; i < N; i++)
    {
        // No linked list at that address
        if (table[i] != NULL)
        {
            // Traverse node
            node *n = table[i]; 
        ...
 }

This eliminates the need for an if/else construction. Note the change from == to != in the if statement.

The code was terminating prematurely because of the null table[] entry. With the full dictionary, it may or may not have encountered this problem, depending on whether all the table[] array elements were populated with words.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

1
  • Thank you so much! – Kalahari May 22 '20 at 14:33

You must log in to answer this question.

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