0

I have my code semi working (it passes most of the check50) except Valgrind keeps reporting 'Invalid read of size 1' . Here is a screenshot of the error message: enter image description here

. I used the djb2 hash function. Here is the snippet of code which seems to be causing the problem:

unsigned long hash(char *str)
{
    unsigned long hash = 5381;
    int c;

    while ((c = *str++))
        hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
    return hash;
}

Any help/ suggestions would be EXTREMELY appreciated. I have been working on this for quite some time and feeling a tad defeated :( .

After looking more into it. The real problem is in my load function:

    bool load(const char *dictionary)
{
    FILE *file = fopen(dictionary, "r");
    if (file == NULL)
        return false;

    // prepare to read words
    int index = 0;
    char* word = malloc(LENGTH + 1);

    // read each word in dictionary
    for (int c = fgetc(file); c != EOF; c = fgetc(file))
    {
        // Allow only alphabetical characters and apostrophes
        if (isalpha(c) || (c == '\'' && index > 0))
        {
            // Append character to word
            word[index] = c;
            index++;
        }

        // We must have found a whole word
        else if (index > 0)
        {
            // Terminate current word
            word[index] = '\0';

            // hash word to find which linked-list bucket it should be added to
            unsigned long bucket = hash(word) % HASH_TABLE_SIZE;

            // insert word into bucket
            hash_table[bucket] = insert(hash_table[bucket], word);

            // Update counter
            words++;

            // Prepare for next word
            index = 0;
            word = malloc(LENGTH + 1);
        }
    }



    // Check whether there was an error
    if (ferror(file))
    {
        fclose(file);
        printf("Error reading %s.\n", dictionary);
        unload();
        return 1;
    }

    // Close text
    fclose(file);

    return true;
}

Specifically the line char* word = malloc(LENGTH + 1);I have tried freeing the memory by including free(word) before I close the file but it doesn't do the trick. Alternatively, I have tried to use char word[LENGTH+1]; so I don't need to free any memory, however, that fails to compile because of an out of index array error. Thoughts?

7
  • And line 154 of dictionary.c is what? – DinoCoderSaurus Apr 24 '20 at 15:09
  • oh sorry, it is: while ((c = *str++)) – janecrager Apr 24 '20 at 15:45
  • Check that the argument sent to hash is properly allocated and properly null terminated. Assuming there are two calls, it's the one that originates at speller line 40. – DinoCoderSaurus Apr 24 '20 at 15:55
  • I'm more interested in line 85 in dictionary.c – Cliff B Apr 24 '20 at 16:11
  • I included the code which seems to cause all the problems... this line char* word = malloc(LENGTH + 1); and then again here word = malloc(LENGTH + 1); – janecrager Apr 24 '20 at 16:19

You must log in to answer this question.

Browse other questions tagged .