1

I am not sure why the words are all coming back misspelled, so I will post the global, check, and load. The run time shows that size and unload never get called. Any help is greatly appreciated. (PS. First time posting, so if I broke any rules I apologize).

bool unload(void)
{
 // TODO
 int i = 0;

while(i < TABLE_SIZE)
{
    if (hash[i] == NULL)
    {
        i++;
    }
    else
    {
        while(hash[i] != NULL)
        {
            node* follow = hash[i];
            free(follow);
            hash[i] = follow->next;

        }

        i++;
    }
}

return true;

}

Here is my new load function (hash_v is my hash function that uses the sum of the ASCII values of a words letters to determine the index)

bool load(const char* dictionary)
{

int index = 0;

FILE* input = fopen(dictionary, "r");

if(input == NULL)
{
   printf("didn't open\n");
   return false;
}

 char buff[LENGTH + 1];

 int len = strlen(buff);

 buff[len - 1] = '\0';         

   while (fscanf(input, "%s\n", buff) != EOF)
   {
        index = hash_v(buff);

        node* new = malloc(sizeof(node));

        strcpy(new->word, buff);

        total++;

        if (new == NULL)
        {
            exit(1);
        } 

    // check if hash[x] is empty
    if (hash[index] == NULL)
    {
        hash[index] = new;
        new->next = NULL;                  
    }
    else
    {

         new->next = hash[index];
         hash[index] = new;  
    }

    }

fclose(input);

return true;

}
1

All functions get called from speller.c unless you modified it yourself. The runtimes are bound to be 0 for size and while unload usually takes a bit longer, the issue is that most likely you have nothing to free there.

The problem is exactly that: You have this line in load:

free(new);

"new" being the memory you malloced a few lines above, and the pointer where you're actually storing the word. Why are you freeing it there?

As an aside, think very carefully what you're doing in check here:

for(int i = 0; i < 1; i++)
{
    if (isalpha(word[i]) && isupper(word[i]))
    {
        temp_txt[i] = tolower(word[i]);
    }
}

what happens when the character is lower case already or not alphabetical? If the word is "Hello", what's temp_txt going to have after the for loop is done?

2
  • Thank you Irene! I now free new->word there. The only issues now are that all words come back misspelled still. I am not sure whether it is in my load function or check function. Also, the word count for dictionary is 143,091; So I am missing two words. And finally, valgrind reports 143,094 errors. Two of the 3 errors come from the "int len = strlen(buff)" line. It says "Conditional jump or move depends on uninitialised value". – Nick Brigham Nov 6 '15 at 13:40
  • But you mean you're still freeing memory in load? That you shouldn't do. You need that memory to stay valid until you're done checking all words – Irene Nov 6 '15 at 23:21

You must log in to answer this question.

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