0

I am struggling to find the problem in my check function. I have a segmentation fault that it's position varies every time I alter my code a bit.

bool check(const char *word)
{
    int index = hash(word);
    if (table[index] == NULL)
    {
        return false;
    }
    node *word_comparison = table[index];
    while (true)
    {
        if (strcasecmp(word_comparison->word, word) == 0)
        {
            return true;
        }
        else if (word_comparison->next == NULL)
        {
            return false;
        }
        else
        {
            word_comparison = word_comparison->next;
        }
    }
}
10
  • Try making the words lowercase first maybe your getting different index for 'You' and 'you', remove your else if statement just put the return false at the end of the function, remove the else scope just leave it like word_comparison = .... after your if statement. – Ojou Nii May 21 '20 at 2:06
  • Which words exactly do you want me to make lowercase? I am using strcasecmp to compare two words without case. – Omar Zayed May 21 '20 at 8:40
  • Yes you are but look at what your passing to your hash function? You are passing a string that might not all be lowercase but all your words in the hash table are lowercase, so for example hash('you') returns 1 while hash('You') returns 5 which would be 2 different indexes. – Ojou Nii May 21 '20 at 12:21
  • That's why my hash function checks if the first letter is capital or not. – Omar Zayed May 21 '20 at 13:43
  • unsigned int hash(const char *word) { if (word[0] <= 'A' && word[0] >= 'Z') { return ((int) word[0] - 65); } else { return ((int) word[0] - 97); } } – Omar Zayed May 21 '20 at 13:48
0

You are trying to get the next pointer here: else if (word_comparison->next == NULL) by comparing it to NULL, but if you didn't set it to NULL in your load function, it'll throw an error since the content of that part of the memory would be random.

Other solution would be the use of calloc instead of malloc, which is the same, but filling up the reserved memory with 0s. In the cs50 course, they don't mention it, maybe because they want you to inicialite your variables consciously.

5
  • But that didn't solve the problem – Omar Zayed May 21 '20 at 20:00
  • Sorry mate. With that little info I can't help you more. – Tritum May 21 '20 at 20:16
  • Thanks anyway. But if I didn't reach a solution at all what should I do. I have posted in many places and I don't really get any help. – Omar Zayed May 21 '20 at 20:28
  • Definitely offer more information about your problem. When do you get the segmentation fault? have you try to check it with debug50 in the CS50 IDE? if positive: what line does it stop? If not, you should give it a try. When people offer you a solution and it doesn't work, you should share the changes you've made, and explain if there is any change in the behavior of the program. In your case you assumed that the fault was in your check function: why do you think that? You get the idea... Also you may have to share the rest of your code, just in case the fault comes from other function... – Tritum May 21 '20 at 20:47
  • I said it was in the check function because I used Valgrind – Omar Zayed May 22 '20 at 7:36

You must log in to answer this question.

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