0

After struggling through the load function and finally getting that correct, I've hit another brick wall with check.

Here's my dictionary.h:

/**
 * Declare struct and global variables for loading words into dictionary and counting total words
 */
typedef struct node
{
    bool is_word;
    struct node* children[27];
}
node;

node* root;

unsigned int dictWords;

Moving on to my check function:

/**
 * Returns true if word is in dictionary else false.
 */
bool check(const char* word)
{
    // declare traversal pointer
    struct node* pTrav = NULL;

    // set traversal point equal to root to begin a new word
    pTrav = root;

    for (int i = 0, c = strlen(word); i < c; i++)
    {
        if (i < c-1)
        {
            if (isalpha(word[i]))
            {
                int x = word[i];
                if (x >= 65 && x <= 90)
                {
                    x += 32;
                }
                int n = x - 'a';
                pTrav = pTrav->children[n];
                if (pTrav == NULL)
                {
                    return false;
                }
            }
            else
            {
                int n = 26;
                pTrav = pTrav->children[n];
                if (pTrav == NULL)
                {
                    return false;
                }
            }
        }
        if (i == (c-1))
        {
            if(!(isalpha(word[i])))
            {
                return false;
            }
            int x = word[i];
            if (x >= 65 && x <= 90)
            {
                x += 32;
            }
            int n = x - 'a';
            pTrav = pTrav->children[n];
            if (pTrav->is_word == true)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
    return false;
}

I'm not entirely confident in my manipulation of const char* word, so I wonder if that's the issue. I can't seem to find an error with my pointer logic (although I had to fix a glaring one to get load to work as I explained here). GDB isn't much help because it just stops before even jumping into check when I go line by line. All I see when I run GDB without a breakpoint is "MISSPELLED WORDS" followed by "Segmentation fault." Any help would be greatly appreciated!

1

This is a case where isolating the seg fault with printf statements is a little more effective than running it through gdb. The seg fault is being exposed by a specific data set. I'll leave it to you to map out the data that's producing the error, and to understand why, but here's the code problem. In your code, you have:

    if (i == (c-1))
    {
        ...
        int n = x - 'a';
        pTrav = pTrav->children[n];
        if (pTrav->is_word == true)
        ...

Running against holmes.txt, you get a condition where pTrav = pTrav->children[n]; is executed, but pTrav->children = NULL. When you then try to access pTrav->is_word in the if statement, it seg faults. Trying to access a component of a nonexistent structure generates a seg fault.

I'll leave it to you to figure out how to remedy this, but it's an easy fix. ;-)

You have similar code earlier that probably has the same problem, although I didn't exhaustively test for it.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

2
  • Thanks for the feedback. That makes total sense to me now. I'm going one step further than I want to with my pointer. I think this is also happening in my load function. Even though it loaded the correct number of words, I took it one step too far in setting is_word which creates a huge hole when it comes to checking spelling. Now, I'm going to rip out what I have, re-watch some lecture clips, and write things out by hand before improving my implementation. Cheers! – Peter Feb 4 '16 at 16:42
  • Oh, I don't know that you need to rip everything out. Maybe it just needs a tweak or two. ;-) (Keep in mind that I haven't done a full analysis of all of your code.) – Cliff B Feb 4 '16 at 19:12

You must log in to answer this question.

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