Ошибка Valgrind: условный переход или перемещение зависят от неинициализированных значений в массиве символов, даже если массив инициализирован - PullRequest
1 голос
/ 20 июня 2020

Когда я запускаю этот код, я получаю сообщения об ошибках: Условный переход или перемещение зависит от неинициализированного значения (значений) и использования неинициализированного значения размера 8, оба в строке 50. Я понимаю, что я не ввел никаких символов / значений в "lcword", но это не должно быть проблемой, потому что я не присваиваю неинициализированные значения чему-либо, я помещаю некоторые символы в массив, чтобы заменить эти унифицированные значения. Я также не вижу неинициализированных значений в условии - инициализировано только j. Как мне решить эту проблему?

unsigned int hash(const char *word)
{
    char* lcword = malloc(46 * sizeof(char));
    for (int j = 0; j < 46; j++)
    {
        lcword[j] = tolower(word[j]); //This is line 50
    }

    unsigned int hash = 0;
    for (int i=0, n=strlen(lcword); i<n; i++)
    {
        hash = (hash << 2) ^ lcword[i];
    }
    free(lcword);
    return hash % N;
}
//Source of hash function: https://github.com/andycloke/Harvard-CS50-Solutions/blob/master/pset5/speller/dictionary.c

Вот весь файл (функция ha sh вызывается в функциях проверки и загрузки):

// Implements a dictionary's functionality
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <ctype.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 = 65536;

// Hash table
node *table[N];
unsigned int sizeofdic = 0;

// Returns true if word is in dictionary else false
bool check(const char *word)
{
    int i = hash(word);
    node *cursor = table[i];
    while (cursor != NULL)
    {
        if (strcasecmp(cursor->word, word) == 0)
        {
            return true;
        }
        else
        {
            cursor = cursor->next;
        }
    }
    return false;
}

// Hashes word to a number
unsigned int hash(const char *word)
{
    char* lcword = malloc(46 * sizeof(char));
    for (int j = 0; j < 46; j++)
    {
        lcword[j] = tolower(word[j]);
    }

    unsigned int hash = 0;
    for (int i=0, n=strlen(lcword); i<n; i++)
    {
        hash = (hash << 2) ^ lcword[i];
    }
        free(lcword);
        return hash % N;
}


// Loads dictionary into memory, returning true if successful else     false
bool load(const char *dictionary)
{
    FILE *dict = fopen(dictionary, "r");
    node *newnode;
    int index;
    char *newword = malloc(46*sizeof(char));

    while (fscanf(dict, "%s", newword) != EOF)
    {
        sizeofdic++;
        newnode = malloc(sizeof(node));
        strcpy(newnode->word, newword);
        index = hash(newnode->word);
        newnode->next = table[index];
        table[index] = newnode;
    }
    fclose(dict);
    free(newword);
    return true;
}

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

// Unloads dictionary from memory, returning true if successful else     false
bool unload(void)
{
    node *cursor;
    node *tmp;

    for (int i = 0; i < N; i++)
    {
        cursor = table[i];
        while (cursor != NULL)
        {
            tmp = cursor;
            cursor = cursor->next;
            free(tmp);
        }
    }
    free(cursor);
    return true;
}

1 Ответ

0 голосов
/ 21 июня 2020

По крайней мере, одна проблема такая: j < 46. Во многих (большинстве?) Случаев это чтение после конца слова.

...