0

I get a seg-fault when I execute: ./speller ~cs50/pset5/texts/austinpowers.txt > student.txt

I think the problem is at Line 115-116 in Check() but I don't know why I have a memory access violation here:

        cursor = cursor->children[hash];
        if (cursor->children[hash] == NULL)

Also, every word is misspelled.

Valgrind Output: http://pastebin.com/WVcfWZ0c

C Code(1 month to expiry): http://pastebin.com/eHdbUA2Z

Pseudo-code: http://pastebin.com/jDupcUb4

Edit: I fixed the program and it's working now. See comments.

2
  • why are you setting unsigned int length = strlen (word) - 1; and then, saying if i = length-1? If the word is apple, then strlen returns 5, so length is 4, so you are checking if i = 3 check is_word. But that's not the end of the word? – curiouskiwi Aug 19 '15 at 4:37
  • Can you include your whole code? I mean the whole dictionary.c and dictionary.h files. Not just snippets. It's really difficult to debug your code like that. We will remove any excess code when your question gets answered. – ChrisG Aug 19 '15 at 8:46
1

You have some really weird construction of your code.

You declare hash on top and assign to it 0, and then change it based on what its previous value was or something? I don't get.

Your hash function should be based on the words you read, so you should probably use the ch char you read by each word, which you then assign to key in your hash function, but somehow you never use. Maybe you should rethink your code.

0
0

I fixed the program. The first major problem is here:

cursor = cursor->children[index_]; 
if (cursor->children[index_] == NULL) 
{
            return false;
}

The cursor enters a node before checking to see that it's not NULL, causing a seg. fault.

There is also an off-by-1 error in this section of check:

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

The check function checks for is_word 1 letter too early, so I added another if/else conditional that checks to see if the next letter is NULL and then descends into the next node before checking is_word.

I also fixed another off-by-1 error with the length variable mentioned by a previous poster.

My procedural mistake was focusing too much on the logic of my code when I could have solved these problems by testing examples(even example memory addresses) in my pseudo-code and gdb.

I'm not sure if this is instructive for other students. Feel free to leave it open if so. Thanks for the help.

You must log in to answer this question.

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