0

Most of this pset5 has gone smoothly, but unload() has caused me problems. When I'm navigating down my data structure (trie) to locate a node with all NULL children, the last index has a memory address for some reason.

The example is the word aaas from the dictionary. There are no other words with characters after the 's', so all children of its node pointer are NULL as expected with the exception of the last index (children[26]) which would equate to the apostrophe character.

In debugging, I watched the load() function allocate memory for this word and an apostrophe character was definitely not allocated. It's stalling out my unload() function! Does this make sense to anyone?

Thanks in advance

int delete_NULL_children(void)

{

int notNULL_index = allNULL?(trav);

//If there are only NULL children on the node, this deletes the node
if (notNULL_index == -1)
{
    node *delete_ptr = trav;

    //travel back up the trie to parent node
    trav = trav->parent;

    free(delete_ptr);

    //recur until root level node is deleted
    if (root != NULL)
        delete_NULL_children();
    else
        return 1;
}
else
{
    //navigate further down our dictionary
    trav = trav->children[notNULL_index];

    //recur until root level node is deleted
    if (root != NULL)
        delete_NULL_children();
}

return 1;

}

3
  • Did you get a Segmentation fault when you accesed the last pointer? Dec 29 '17 at 16:00
  • Not exactly. If I run the program without breakpoint debugging, it suffers a segmentation fault. But what actually happens stepping through is that it plumbs down the trie from Root---a---a---a---s. Then, instead of detecting only NULL children of s, it finds memory at index 26, an apostrophe. Then it's supposed to plumb down that node but instead it somehow gets back up to the s node. And then it just continues to ping-pong between s and ' more times than I can manually step through to make it sigsegv, but yes, when run without breakpoints it eventually sigsegvs.
    – Scott J.
    Dec 29 '17 at 16:59
  • uploaded some code to clarify further. notNULL_index will return -1 if all nodes tested are NULL which I would expect it to do when reaching 's' node. Instead it finds memory allocated at children[26]. And again, my load function doesn't show anything being allocated for children[26]. After allocating for the 's' node, it moves on to the next word
    – Scott J.
    Dec 29 '17 at 17:29
0

I forgot to update the index number earlier in my program. Couldn't really see that from the code I posted.

You must log in to answer this question.

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