2

"debug50 ./speller texts/alice.txt" doesn't return a segfault. It runs to completion. But "./speller texts/alice.txt" returns a segfault. Can I PM someone my code to take a look at? Why would running my program outside the debugger (GDB) return a segfault while debugging doesn't return a segfault?

Also 'words in dictionary,' and 'words in text' are counted correctly but 'words misspelled' isn't, so I'd like help here, too, please. Here's my check function (with globals added to top): http://pastebin.com/cL49aTrA

edit: I just read up a bit on Heisenbugs and understand why it wouldn't seg fault in a gdb environment, but it still doesn't tell me why my code is segfaulting.

2

You can jump to SUGGESTION for quick answer.

I plugged your check() function on my dictionary.c program. I also have a trie in my dictionary.c.

I don't know what is causing the segmentation fault. However, I found a behaviour that might be of your interest.

I ran the program with both my check() function and yours against alice.txt. I found that among the words that differed from our output, "org" was one of them.

I checked with gdb using a text that only contained that word. My check() function considered it misspelled, and yours did not.

With that example I was able to notice the difference (using gdb). Your check() function is evaluating the boolean too soon.

Based on your pastebin file, on lines 58-61 you have:

if (leaf->is_word == true)
{
    return true;
}

But at that point you are not at the end of the word you are checking. Look at it here with the context:

for (int i = 0, n = strlen(word); i < n; i++)
{

    int alphaplace2 = tolower(word[i]) - 'a';

    if (word[i] == '\'')
    {
        alphaplace2 = 26;
    }

    if (leaf->children[alphaplace2] != NULL)
    {

        leaf = leaf->children[alphaplace2];

        //**CHECKING BOOLEAN**

        if (leaf->is_word == true)
        {
            return true;
        }

        //**CHECKING BOOLEAN**

    }
    else
    {
        return false;
    }
}

You want to check if it is a word after you know that each and every letter is in your trie in the right order.

If you check at every letter you will find a problem if your dictionary has:

cat
caterpillar

And your text has only this line:

caterina

I already tested this and the speller does not show caterina as misspelled. This is because the check() function is checking the boolean at every letter. And it does find a true is_word at the end of cat.

SUGGESTION

I suggest you move your boolean check outside your loop. For example:

bool check(const char* word)
{

    node* leaf = root;

    for (int i = 0, n = strlen(word); i < n; i++)
    {

        int alphaplace2 = tolower(word[i]) - 'a';

        if (word[i] == '\'')
        {
            alphaplace2 = 26;
        }

        if (leaf->children[alphaplace2] != NULL)
        {
            leaf = leaf->children[alphaplace2];
        }
        else
        {
            return false;
        }
    }

    //**NOTICE BOOLEAN CHECK OUTSIDE LOOP**

    if (leaf->is_word == true)
    {
        return true;
    }

    //**NOTICE BOOLEAN CHECK OUTSIDE LOOP**    

    return false;
}
| improve this answer | |
  • thanks, brother! you hit the nail on the head for check(). now if I could only figure out the seg fault >< – Spenny Davis Nov 9 '16 at 1:14
  • Thanks to you for the opportunity! : ) The fact that the segmentation fault does not happen on my program (even with your check() function) makes me think it is not rooted in the check() function. I would think it is in the load() function even without looking at the code. That is where a segmentation fault would be expected to happen in this pset. – Ricardo David Antonetti Nov 9 '16 at 12:58

You must log in to answer this question.

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