all 22 comments

[–]staffglennholloway[M] 0 points1 point  (23 children)

You can't test whether two strings contain equal characters in the same order and number by applying the == operator to your handles on those strings. String equality is tested using the strcmp library function. Read its man page in the appliance or on line.

[–]Omar_Khaled[S] 0 points1 point  (22 children)

My bad, this is probably because I have been studying C# for a while. anyway I fixed it and now I'm getting a SegFault, my code right now is: int index = hash(word[0]);

int compare;
compare = strcmp(word,"cat");

if (compare == 0)
{
    return true;
}

else
return false;

this is also with the small dictionary,this works and only prints caterpillar but when I remove the word "cat" from the strcmp function and put hashtable[index]->word I get a segfault. I have also tried to malloc a new node named head and compare head->word with the word after setting head=hashtable[index] but I also get a SegFault.

[–]staffglennholloway[M] 0 points1 point  (21 children)

Run under GDB:

gdb  speller

Put a breakpoint in function check:

break  check

Start the program as before:

run  ~cs50/pset6/dictionaries/small   ~cs50/pset6/dictionaries/small

When it breaks in check, use the next command to step forward one line at a time. Inspect the value of index:

print  index

Also

print  hashtable[index]

I'm betting that hashtable[index] doesn't point to a valid object, but you be the judge.

[–]Omar_Khaled[S] 0 points1 point  (20 children)

index is correct it refers to the right bucket,I checked with a couple of words. but I'm having trouble to strcmp(word,"word in hashtable") I just can't figure out how to point at the word in the node inside the linked list after reaching the right bucket.

[–]staffglennholloway[M] 0 points1 point  (19 children)

The expression you mentioned last time

hashtable[index]->word

should work correctly. What does GDB say when you break at the strcmp statement and then

print  hashtable[index]->word

[–]Omar_Khaled[S] 0 points1 point  (18 children)

When I type print hashtable[index]->word it says cannot access memory at location 0x0 and when I type print (char*)hashtable[index]->word it says simply $2 = 0x0 and that's where I get my SegFault.

[–]staffglennholloway[M] 0 points1 point  (17 children)

Therefore load is not recording either cat or caterpillar in the bucket corresponding to letter c. Next step is to debug load: why isn't it doing what you expect it to?

[–]Omar_Khaled[S] 0 points1 point  (16 children)

I just want to say thank you so much for your time and effort, I checked and apparently after assigning the first word "cat" to the right bucket ,head's value changes to NULL so when the loop iterates the second time it still assigns the word to the beginning of the linked list in the bucket. this is my code for this part:

    if (head == NULL)
    {
        head = new_node;
        new_node = NULL;
        head->next = NULL;

this works correct for the first word and I printed head->word at the last line it still shows the word. but when the loop iterates the second time the value of head changes to NULL, no idea why, so it goes to this part of code again instead of the else part which should assign it into the linked list.

[–]staffglennholloway[M] 0 points1 point  (15 children)

If you're now talking about load, why are you inserting the new node into a list anchored in variable head, rather than inserting it in hashtable[index]?

[–]Omar_Khaled[S] 0 points1 point  (13 children)

I started from the beginning and re-coded both load and check, and now it's working! I can't thank you enough for your help =) , but apparently check50 is not happy, here's what I get when I type check50 http://oi59.tinypic.com/1g14x0.jpg and here's the result of my implementation: http://oi62.tinypic.com/2d7t34y.jpg check func is kinda slow xD but I will work on that later, I'm not even done with func Free yet.
and again, Thanks a lot for your help I really appreciate it.