0

I am almost done with CS50 (working on the final project now) but one PSET of mine is incomplete. I can get it to compile and run, but Valgrind is not happy. Here's a snipped of the most relevant code and valgrind summary. Here's the full thing: https://pastebin.com/VH0ejUXM

bool load(const char *dictionary)
{

    root = malloc(sizeof(node));
    if(root == NULL)
        return false;
    for(int i = 0; i < 26; i++)
    {
        root->letter[i] = NULL;
    }
    root->isword = false;


    FILE* fp = fopen(dictionary, "r");

    // check for an unsuccessful open
    if (fp == NULL)
            return false;

    // set a buffer to store an output word of length = LENGTH + 1 for null terminator
    char curline[LENGTH+2];

    // loop through the dictionary until a null character
    while (fgets(curline, sizeof(curline), fp))
    {
        char c = '\0';
        curline[strlen(curline)-1] = c;
        insert(curline);
        wordcount++;
    }

    fclose(fp);
    return true;
}

bool unload(void)
{
    node *ptr = root;
    byebye(ptr);

    return true;
}

void byebye(node *delnode)
{

    node *ptr = delnode;

    for(int i = 0; i < 27; i++)
        if(ptr->letter[i] != NULL)
            byebye(ptr->letter[i]);

    free(delnode);

}

==1859== LEAK SUMMARY:
==1859==    definitely lost: 0 bytes in 0 blocks
==1859==    indirectly lost: 0 bytes in 0 blocks
==1859==      possibly lost: 0 bytes in 0 blocks
==1859==    still reachable: 78,291,032 bytes in 349,512 blocks
==1859==         suppressed: 0 bytes in 0 blocks
==1859== Reachable blocks (those to which a pointer was found) are not shown.
==1859== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==1859== 
==1859== For counts of detected and suppressed errors, rerun with: -v
==1859== ERROR SUMMARY: 349491 errors from 3 contexts (suppressed: 0 from 0)

Thank you!

2

Valgrind is crashing as seen here:

==1737== Process terminating with default action of signal 11 (SIGSEGV)
==1737==  Access not within mapped region at address 0xA1
==1737==    at 0x401322: insert (dictionary.c:120)
==1737==    by 0x40128D: load (dictionary.c:84)
==1737==    by 0x40095D: main (speller.c:40)
==1737==  If you believe this happened as a result of a stack
==1737==  overflow in your program's main thread (unlikely but
==1737==  possible), you can try to increase the size of the
==1737==  main thread stack using the --main-stacksize= flag.
==1737==  The main thread stack size used in this run was 8388608

That's why your unload function actually never runs.

Pay attention to the errors that you are getting before this, that you are using uninitialized values. You're mallocing a node in your insert function, but you are never initializing the memory. Maybe use calloc?

Also, what happens if the char is an apostrophe? Won't you end up with a negative index into your array? Boom, you hit memory that you have no access to, and it crashes.

So, first, fix the uninitialized problem, then fix the apostrophe issue, then try again.

1
  • Here's an important item to remember. If the program still has bugs, then using valgrind may be premature. While it may give you clues to what is going wrong, a lot of memory leaks due to bugs will go away when they're fixed. Once the program is doing what it is supposed to do, then valgrind will reveal true memory leaks. (Memory leaks often come from code that works correctly, but which has left something undone or unaccounted for.) – Cliff B Aug 9 '17 at 2:26

You must log in to answer this question.

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