0

Is there anyone that can help with my check function on pset5. I've got load working fine and confirmed that the correct number of words are loading in both the small and large dictionaries. I've set a copy of the word so that I can change the letter to lowercase if it's upper case. The a-1 is so that an apostrophe (ascii code 96 and one before a) is assigned to index 0 and then the others are assigned up 1 through 27.

I'm not quite sure where the seg fault would occur. I'm trying to loop through each letter of the word, and following the pointers through the trie and then once the loop is complete I'm asking to check whether the pointer is pointing to a NULL and if not then move on. At the end of the word, check whether is_word is true or not.

Is the pointer going one level too far or something? I can't quite find the seg fault. Thanks in advance for any help.

bool check(const char* word)
{
//set trav to root
node* trav = root;
char word2[strlen(word)];
strcpy(word2, word);

//loop through each letter of the word
    for (int i = 0 ; i < strlen(word2) ; i++)
    { 
        //set index using to lower.  Use a-1 to set ' to 0 and other letters 1-27
        int index = tolower(word2[i])-('a'-1);

        //if trav-> children is NULL then return false
        if (trav->children[index] == NULL)
        {
            return false;
        }

        //else pointer must exist so move trav on
        else {
            trav = trav->children[index];
        }
    }

//exists for loop at the end of the word.  Check if it's a word.    
if (trav->is_word == true)
{
 return true;
}

else {
 return false;
}

return false;
}
3
  • Just to add to this, I ran GDB and the set fault is occurring on the line "if (trav->children[index] == NULL)". I globally declared root and then pointed trav to root so I'm not sure why it would set fault trying to check if there is a null in trap->children[index]
    – jamiej
    Jul 14 '16 at 8:36
  • I just figured out the answer (in case anyone has a similar issue). And actually the error was in Load so apologies for not including that (I didn't want to paste too much of the pset in). I declared root globally but in load I'd calloc'd the memory using *node root = calloc rather than just root = calloc which meant that there was a new local instance with my trie set up. Thanks for anyone that had a look at the above and I hope this helps someone else in future. Cheers.
    – jamiej
    Jul 14 '16 at 9:09
  • Glad you found the answer. Could you post your solution as an answer and accept it (in 2 days - system requirement) to remove this question from the unanswered question pool please? thanks
    – Cliff B
    Jul 14 '16 at 17:33
2

I just figured out the answer (in case anyone has a similar issue). And actually the error was in Load so apologies for not including that (I didn't want to paste too much of the pset in). I declared root globally but in load I'd calloc'd the memory using *node root = calloc rather than just root = calloc which meant that there was a new local instance with my trie set up. Thanks for anyone that had a look at the above and I hope this helps someone else in future. Cheers

You must log in to answer this question.

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