简体   繁体   中英

Memory errors in CS50 speller (pset5)

I have successfully written a code that can spell check from loading a dictionary. To free the memory, I have written the unload() function and valgrind shows no memory leaks. But after submission, I am getting some cryptic errors, ie

Conditional jump or move depends on uninitialised value(s): (file: dictionary.c, line: 137)
Conditional jump or move depends on uninitialised value(s): (file: dictionary.c, line: 143)

Below are my unload() function, valgrind screenshot and submit50 result respectively. Please help.

Thanks.

//unload function

bool unload(void)
{
    for (int i = 0; i < N; i++)
    {
        node* temp = table[i];
        node* cursor = temp;
        while (temp != NULL)
        {
            cursor = temp->next;
            free(temp);
            temp = cursor;
        }
        free(cursor);

    }
    return true;
}

// valgrind result

// submit50 result

It's because temp -> next is uninitialized for some cases.

You could make it the last node in each table bucket point to NULL.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM