0

I am doing pset 5 speller and It seems to be okey less valgrind check.

Valgrind HELP50 gives me these errors

==22585== Use of uninitialised value of size 8 ==22585== at 0x40110B: hash (dictionary.c:54) ==22585== by 0x401249: load (dictionary.c:97) ==22585== by 0x400964: main (speller.c:40)

Here's my code:

unsigned int hash(const char *word)
{
    int n_bucket = 0;
    for(int i = 0; i < 4; i++, n_bucket += 28)
    {
        // THE LINE BELOW IS THE 54th !!!!!
        if (isalpha(word[i]) != 0)
        {
            int suma_hash = (tolower(word[i]) - 'a') % 26;
            n_bucket += suma_hash;
        }
        else
        {
            n_bucket += 27;
        }

    }

    n_bucket -= 56;
    return n_bucket;
}




bool load(const char *dictionary)
{
    // fopen
    FILE *file = fopen(dictionary, "r");

    char word[LENGTH];

    if (file == NULL)
    {
        printf("No dictionary.\n");
        return false;
    }
    else
    {
        //read string from file one at a time
        while (fscanf(file, "%s", word) != EOF)
        {
            node *n = malloc(sizeof(node));
            if (n == NULL)
            {
                return false;
            }
            else
            {
                strcpy(n->word, word);
                n->next = NULL;

                // THE LINE BELOW IS THE 97th !!!!!
                int where = hash(n->word);

                if (table[where] != NULL)
                {
                    n->next = table[where];
                    table[where] = n;
                }
                else
                {
                    table[where] = n;
                    n->next = NULL;
                }
            number_words += 1;
            }
        }
        return true;
        fclose(file);
    }

}

Anyone could guide me to understand what i am doing wrong?

Thank you for the time, Best regards, Santiago Correa

0

What was the data passed into the pointer word?

1
  • char word[LENGTH] or do you mean the return of the hash function? – santiago correa Aug 2 '20 at 5:17

You must log in to answer this question.

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