0

I've been struggling with this pset for long and still no luck. My code works with small dictionary but when using large dictionary unload function gives me seg-fault (I hard coded return true in unload and everything goes fine and it even finds misspelled words). After debugging I noticed that first with large dictionary it gives me "a" as misspelled even if it is in dictionary and in text. So I checked the debugger and noticed the with large dictionary the value of is_word in trie for character 'a' is changed to 118. I'm really confused about this behavior I even tried to compare the value of is_word with 118 but I got an error indicating type mismatch between bool and int. Here I paste some parts of load so that may some one can help me about this:

while (fgets(word, LENGTH + 1, dic))
{
    char character;
    node* curser = root;
    if (word == NULL)
    {
        return false;
    }
    word[strlen(word) - 1] = '\0';
    for (int i = 0, len = strlen(word); i < len; i++)
    {
        character = word[i];
        if (character == '\'')
        {
            if (curser->children[26] != NULL)
            {
                curser = curser->children[26];
            }
            else
            {
                node* new_node = (node*)malloc(sizeof(node));
                curser->children[26] = new_node;
                curser = curser->children[26];
            }
        }
        else
        {
            if (curser->children[(int)character - 97] != NULL)
            {
                curser = curser->children[(int)character - 97];
            }
            else
            {
                node* new_node = (node*)malloc(sizeof(node));
                curser->children[(int)character - 97] = new_node;
                curser = curser->children[(int)character - 97];
            }
        }
        if (i == len - 1)
        {
            curser->is_word = true;
            word_count++;                
            continue;
        }
    }
}

here's my node definition:

typedef struct node
{
    bool is_word;
    struct node* children[27];
}
node;
2
  • how did you define node? – ronga Jul 20 '16 at 7:54
  • hi ronga, I updated my post with node definition. – Farhad Jul 22 '16 at 5:08
0

Your load function looks ok. But from your description of the way is_word is acting strangely, you can try initializing is_word to false when you malloc a node, only setting it to true when you reach the end of a word, as you have done.

You must log in to answer this question.

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