I've been trying to figure this out and I really need help.

When I run speller with a text file against the large dictionary, my code doesn't catch all the misspellings that are supposed to be in the text file. For example, austinpowers.txt is supposed to have 644 mispellings, I get only 300+. I experimented a bit and tried using different hash functions — all functions load the dictionaries correctly, but each returns a different number of mispellings (ranging from 100 to 300+). I can't figure out why that would happen.

  • This only happens when I use the large dictionary though — no problems at all when using the small dict.
  • I'm assuming my load function is correct. I've checked its output against the dictionaries, and it loads the correct number of words.

I'm guessing it's something wrong with my check function. Can anyone look through it please?

p/s. No errors in valgrind at all.

I've tried using gdb, but everything's fine with small files, and I'm not sure if going through large files in gdb would be practical.

My check function:

bool check(const char* word)
    // changes string to lowercase
    char wordCheck[LENGTH+1];
    strcpy(wordCheck, word);

    for (int i = 0; i < strlen(wordCheck) +1; i++)
        wordCheck[i] = tolower(wordCheck[i]);

    // runs word through hash function
    int index = hash(wordCheck);

    // creates a cursor for searching and points it to start of linked list
    node* cursor = table[index];

    // starts searching 
    if (table[index] == NULL)
        return false;

    while (cursor != NULL)
        if (strcmp(cursor->word, wordCheck) == 0)
            return true;
            cursor = cursor->next;

    return false;


Edit: here's my entire code: http://pastebin.com/S7vsLtAN

  • I don't think there's a problem with check above. I still have comments on it though. 1. why copying word into wordCheck then converting it to lowercase? why not iterating over word, converting the letters to lowercase and storing them one by one into wordCheck directly? 2. you don't really need an else statement inside the while loop since if the condition is true, the return statement is executed and the function returns immediately. so you could safely update cursor after the if without an else. you should probably update your question with a copy of your whole code! – kzidane Jun 12 '15 at 18:15
  • Thanks Kareem. I used wordCheck[i] = tolower(word);, but it won't compile, throwing out this error: error: incompatible pointer to integer conversion passing 'const char *' to parameter of type 'int' [-Werror,-Wint-conversion]. I thought const chars can't be changed? (I updated my question with a link to my code.) If you could take a look that would be great! – C. Yen Jun 12 '15 at 23:44
  • you're not passing a char to tolower. – kzidane Jun 12 '15 at 23:53

indeed there is a problem with your load function. two main things:

  1. str is a local variable that goes out of scope as soon as load returns in this case. trying to access the string at any point beyond the scope of str has an undefined behavior (you don't know what's gonna happen).

  2. str is getting reused at every iteration of the loop — you're using the same variable to read each word in the dictionary.

however, you're still assigning it to new_node->word and this does not actually "copy" the string pointed to by str into the memory pointed to by new_node->word. rather this sets the new_node->word to "point" to the memory pointed to by str.

| str | ==============>    +++++++++++++
+++++++                    |           |
++++++++++++++++++         |  "hello"  |
| new_node->word |====>    |           |
++++++++++++++++++         +++++++++++++

so when the value that the str points to gets modified, the value that the new_node->next points to gets modified as well. and when str goes out of scope, the behavior of accessing the string pointed to by new_node->word is undefined.

  • Thanks for the explanation! I'll work on my code based on what you said. – C. Yen Jun 13 '15 at 0:39
  • So this worked! Thanks so much Kareem! I ended up changing my node structure from char* word to char word[LENGTH + 1], and used strcpy to copy into newNode->word. One final question — I can't figure out why this would cause some words to be copied correctly, while others aren't (and hence only some mispellings are caught). What's the reason for this? – C. Yen Jun 15 '15 at 2:06
  • @C.Yen by "this" you're referring to ... ? – kzidane Jun 15 '15 at 12:41

You must log in to answer this question.

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