0

I have a problem making dictionary.c work and since all the functions are related to each other I can't figure out where the error is coming from. when I execute the program I get

*** Error in `./speller': double free or corruption (top): 0x12986418 ***
Aborted (core dumped)

I feel it's a close relative to the segmentation fault but I could be wrong.

I googled and found a similar error that mentioned it had to do with wrongly updated pointers. It's apparent that my output is very wrong considering which words are correct and which aren't but I have no clue whether it's due to load, unload, or check. here's my code:

bool unload(void)
{
    // make crawler pointer and previous pointer
    node* crawler = root;
    node* previous = crawler;

    //initialize int for traversing the trie
    int i = 0;

    // Go through the whole trie structure to the very bottom, delete that node, then start over from the top till you delete everything    
    while (root->children != NULL)
    {
        if (crawler->children[i] != NULL)
        {
            previous = crawler;
            crawler = crawler->children[i];
            i = 0;
        }
        else
        {
            if (i < 27)
            {
                i += 1;
            }
            else
            {
                free(crawler);
                i = 0;
                crawler = previous;
            }
        }
    }

    // return true if everything unloads as expected
    return true;
}

This problem set is very hard to debug because there are so many functions and there's no easy way to isolate them one by one. Thanks for all your help! :)

1
  • Try using gdb to find exactly where your program fails.
    – ChrisG
    Jul 2 '15 at 20:23
1

the error is relating to freeing the memory. it's caused by your unload function which is quite a mess actually. this error is a runtime error that typically happens when you free a pointer twice in a row (without allocating memory for it by the second time you free it).

to show you why the error is produced let's assume that root and root->children[0] have memory allocated for them.

in the first iteration of the while loop, previous is set to crawler which is set to root so previous is set to root as well. next crawler is set to crawler->children[0] which is root->children[0].

the next iteration of the loop is executed and the else statement is executed this time, then you're incrementing i until it becomes 27, then you're freeing crawler, which recall is still root->children[0], then you're assigning previous to it, which recall is root.

since crawler is again equal to root, this process repeats and free is called on the same pointer for the second time. this is when the error pops up on the screen.

calling free on a pointer does not set that pointer to NULL.


fix:

obviously your unload function doesn't do its job correctly. recursion is much easier to use in this situation. here's some pseudocode that may help you

function freeNode(n)
    for each child of n
        if currChild is not NULL
            freeNode(currChild)

    free node
1
  • Thank you so much again!!!!!!!!!!!!
    – Xiggi
    Jul 2 '15 at 23:20

You must log in to answer this question.

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