0

I am working in Speller of PSET5. My program compiles and only has one problem

:( program is free of memory errors
    valgrind tests failed; rerun with --log for more information.

The rest of the check50 items are all in green

When i run valgrind, this is what it says

==4849== 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 33 of dictionary.c.

Can anyone help me? I don´t know how to continue, i have tried to change a lot of lines of code and i don´t find my error. I have already tried to change or inicialized the line 33 of the code which is:

lowerword[i] = tolower(word[i]);

This is my complete code

// Implements a dictionary's functionality

#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.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 //Tenemos que cambiar el N
const unsigned int N = 10000;

// Hash table
node *hashtable[N];


bool check(const char *word)
{
    // Inicializar caso de minuscula
    char lowerword[LENGTH + 1];
    // Convertir a miniscula
    for (int i = 0; i < LENGTH; i++)
    {
        **lowerword[i] = tolower(word[i]);**
    }

    // Poner el cursor al principio de la linked list
    node *cursor = hashtable[hash(lowerword)];

    while (cursor != NULL)
    {
        //Chequear
        if (strcasecmp(word, cursor->word) == 0)
        {
            return true;
        }
        cursor = cursor->next;
    }
    return false;
}

// Hashes word to a number
unsigned int hash(const char *word)
{
    // Buscar hash function en internet -- //
    unsigned int hash_value = 0;

    for (int i = 0, n = strlen(word) ; i < n; i++)
    {
        hash_value = (hash_value << 2) ^ word[i];
    }
        return hash_value % N; //N es el número de buckets de Hashtable
}


// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
    char word[LENGTH+1];
    //Abrir archivo de diccionario
    FILE* dic = fopen (dictionary, "r");
    // Si no lo encuentra , return false
    if (dic == NULL)
    {
        return false;
    }
    //Loop en las palabras del diccionario
    while (fscanf(dic, "%s", word) != EOF)
    {
        //Crear un new node y asignar memoria

        node *new_node = malloc(sizeof(node));
        //Chequear que no sea null
        if (new_node == NULL)
        {
            return false;
        }
        else
        {
        //Copiar la variable word en el new node
        strcpy (new_node -> word, word);

        //Usar the hash function (buscar en internet)
        int h= hash (word);

        //Insertar el nodo en la hash table
        new_node->next = hashtable[h];
        hashtable[h] = new_node;
        }
    }
    fclose(dic);
    return true;
}


// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
    //Contador de palabras para function size
    int cant_palabras = 0;
    //Loop en cada palabra para contarlas
    for (int i = 0; i < N; i++)
    {
        //Incluyo en la hash table
        node *cursor = hashtable[i];

        // traverse list
        while (cursor != NULL)
        {
            cant_palabras ++;
            cursor = cursor->next;
        }
    }
    return cant_palabras;
}

// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
    // Liberar la memoria allocated (free)
   //Loop en la hashtable en cada nodo de cada linked list
     for (int i = 0; i < N ; i++)
    {
        // set pointer to head of list
        node *cursor = hashtable[i];

        //Proceso de liberacion de memoria
        while (cursor != NULL)
        {
            node *temp = cursor;
            cursor = cursor->next;
            free(temp);
        }
        free(cursor);
    }
    return true;
}
0

Valgrind is giving that warning because the variable 'lowerword' initialization, depends of a conditional sentence: i < LENGTH where 'LENGTH' value is uncertain.

You could try something like: i < strlen(word) + 1. The '+ 1' would be for the end '\0' character.

1

You must log in to answer this question.

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