0

I've looked through all the various answers but I can't seem to figure out what the issue is... I understand that I can't reference values that may not be initialized but I don't understand how else to rewrite the code to properly deal with it.

Error logs:

==428== Conditional jump or move depends on uninitialised value(s)
==428==    at 0x483EF58: strlen (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==428==    by 0x401AC4: hash (dictionary.c:67)
==428==    by 0x401A24: check (dictionary.c:42)
==428==    by 0x4015EB: main (speller.c:112)
==428==  Uninitialised value was created by a stack allocation
==428==    at 0x4019D0: check (dictionary.c:36)
==428==
==428== Conditional jump or move depends on uninitialised value(s)
==428==    at 0x49D9143: tolower (ctype.c:46)
==428==    by 0x483F873: strcasecmp (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==428==    by 0x401A6B: check (dictionary.c:53)
==428==    by 0x4015EB: main (speller.c:112)
==428==  Uninitialised value was created by a stack allocation
==428==    at 0x4019D0: check (dictionary.c:36)

And the relevant code:

bool check(const char *word)
{
    char wordCpy[LENGTH + 1];
    for(int i = 0; word[i]; i++)
    {
        wordCpy[i] = tolower(word[i]);
    }
    int key = hash(wordCpy);

    if (table[key] == NULL)
    {
        return false;
    }
    else
    {
        node *temp = table[key];
        while(temp != NULL)
        {
            if (!strcasecmp(temp->word, wordCpy))
            {
                return true;
            }
            temp = temp->next;
        }
    }
    // TODO
    return false;
}

// Hashes word to a number
unsigned int hash(const char *word)
{
    int n = strlen(word);
    unsigned int hash = 0;
    for (int i = 0; i < n; i++)
    {
        hash = (hash << 2) ^ word[i];
    }
    return hash % N;
}

// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
    populateTable();
    FILE *dict = fopen(dictionary, "r");


    if (dict == NULL)
    {
        printf("Could not open %s.\n", dictionary);
        unload();
        return 1;
    }

    char word[LENGTH + 1];

    while (fscanf(dict, "%s", word) != EOF) {
        int hashkey = hash(word);
        node *newNode = calloc(1, sizeof(node));


        if (newNode == NULL)
        {
            return 1;
        }

        for (int i = 0; i < LENGTH + 1; i++)
        {
            newNode->word[i] = '\0';

        }

        strcpy(newNode->word, word);

        newNode->next = NULL;

        unsigned int index = hash(word);

        newNode->next = table[index];
        table[index] = newNode;
        dictSize++;

    }

    if (dict != NULL) {
        fclose(dict);
        return true;
    }

    return false;
}
1
  • Since you didn't identify which lines are the ones causing the problems and didn't include the complete code, it's nearly impossible to duplicate your problem and to identify the issues. Can you please remedy this? – Cliff B Jan 6 at 3:05
0

Since it's complaining about strlen in hash about something created in check attention can be directed to wordCpy. It is being treated as a string (because it is an argument to strlen). But it's not a valid string because it is not properly NULL terminated.

1
  • Ah, thank you! That was exactly it! – Luke Jan 7 at 3:12

You must log in to answer this question.

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