0

I canĀ“t find why valgrind is giving me this message.

** ==3087== Conditional jump or move depends on uninitialised value(s)

Looks like you're trying to use a variable that might not have a value? Take a closer look at line 133 of dictionary.c.

**

// Implements a dictionary's functionality

#include <stdbool.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>

#include "dictionary.h"

// Represents a node in a hash table
typedef struct node
{
    char word[LENGTH + 1];
    struct node *next;
}
node;

// Number of buckets in hash table
const unsigned int N = 26;

// Hash table
node *table[N];

// Returns true if word is in dictionary else false
bool check(const char *word)
{
    //Create a copy of the word
    char word_copy[LENGTH + 1];
    for (int i = 0; i <= strlen(word); i++)
    {
        word_copy[i] = tolower(word[i]);
    }

    //Sets pointer
    node *cursor = table[hash(word_copy)];

    //Check if word in dictionary
    while (cursor != NULL)
    {
        if (strcasecmp(word_copy, cursor->word) == 0)
        {
            return true;
        }
        else
        {
            cursor = cursor->next;
        }
    }

    return false;
}

// Hashes word to a number (hash function from delipity post on reddit)
unsigned int hash(const char *word)
{
    // TODO
    unsigned int hash_value = 0;

    for (int i = 0; i < strlen (word); i++)
    {
        hash_value = (hash_value << 2) ^ word[i];
    }

    return hash_value % N;
}

//Initializes counter for words in dictionary
int word_count = 0;

// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
    // Opens dictionary
    FILE *file = fopen(dictionary, "r");
    if (file == NULL)
    {
    return false;
    }

    //Read strings
    char word[LENGTH + 1];
    while (fscanf (file, "%s", word) != EOF)
    {

        //Allocates node for each word
        node *n = malloc (sizeof(node));
        if (n == NULL)
        {
            unload();
            return false;
        }
        //Copy word if true
        strcpy(n->word, word);

        //Calculate index of word
        int h = hash(n->word);

        //Initialize head of hash table
        node *head = table[h];

        //Isert new node at the beginning
        if (head == NULL)
        {
            table[h] = n;
            word_count++;
        }
        else
        {
            n->next = table[h];
            table[h] = n;
            word_count++;
        }
    }
    fclose(file);
    return true;
}

// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
    // TODO
    return word_count;
}

// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
    for (int i = 0; i < N; i++)
    {
        node *cursor = table[i];
        while (cursor != NULL)
        {
            node *temp = cursor;
            cursor = cursor->next;
            free(temp);
        }
    }
    return true;

}

0

The while loop depends on the value in cursor. In turn, cursor's initial value depends on table[i]. The problem is that table[] was never initialized. The compiler recognizes this as possibly using an uninitialized value.

Try putting a for loop in the beginning of load() to set all the elements in table[] to NULL.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

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