当我编译并运行我的代码时,看起来好像代码可以工作,但是当我使用help50运行Valgrind时,它说 “ == 1295 ==条件跳转或移动取决于未初始化的值” 我不知道如何解决此问题。 Help50告诉我专注于代码的第54行,但是我不明白什么地方出了问题,它在以前是可行的,现在是一个错误。我不明白是什么错误/
#include <ctype.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.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 = 10000;
int i = 0;
// Hash table
node *table[N];
// Returns true if word is in dictionary else false
bool check(const char *word)
{
char lword[LENGTH+1];
for(i = 0; i < strlen(word); i++)
{
lword[i] = word[i];
lword[i] = tolower(lword[i]);
}
node *current;
int hashnum = hash(lword);
if(table[hashnum] == NULL)
return false;
current = table[hashnum];
while(current->next != NULL)
{
if(strcmp(current->word, word) == 0)
return true;
else
current = current->next;
}
return false;
}
// Hashes word to a number
// Hash function from cs50.stackexchange
unsigned int hash(const char *word)
{
int n;
unsigned int hash_value = 0;
for (i = 0, n = strlen(word); i < n; i++)
{
hash_value = (hash_value << 2) ^ word[i];
}
return hash_value % N; //N is size of hashtable
}
// Loads dictionary into memory, returning true if successful else false
// adopted from github user
int word_count = 0;
bool load(const char *dictionary)
{
FILE *file = fopen(dictionary, "r");
if (file == NULL)
{
return false;
}
char word[LENGTH + 1];
while (fscanf(file, "%s", word) != EOF)
{
node *new_node = malloc(sizeof(node));
if (new_node == NULL)
{
free(new_node);
return false;
}
strcpy(new_node->word, word);
int h = hash(new_node->word);
node *head = table[h];
if (head == NULL)
{
table[h] = new_node;
word_count++;
}
else
{
new_node->next = table[h];
table[h] = new_node;
word_count++;
}
}
fclose(file);
return true;
}
// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
return word_count;
}
// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
for(i=0;i<10000;i++)
{
for(i = 0; i < 10000; i++)
{
while(table[i] != NULL)
{
char *retval = NULL;
if (table[i]->next == NULL)
{
retval = table[i]->word;
free(table[i]);
return retval;
}
else
{
node * current = table[i];
while (current->next->next != NULL)
{
current = current->next;
}
retval = current->next->word;
free(current->next);
current->next = NULL;
}
}
}
}
return true;
}
答案 0 :(得分:0)
您有很多问题。导致valgrind
基于未初始化值的条件移动的主要问题是您无法初始化->next
中的NULL
指针load()
,例如:
node *new_node = malloc(sizeof(node));
if (new_node == NULL)
{
// free(new_node); /* not necessary */
return false;
}
strcpy(new_node->word, word);
new_node->next = NULL; /* must initialize NULL */
这将解决valgrind
问题。
C不是C ++。 const unsigned int N = 10000;
不会创建整数常量,导致node *table[N];
是指向node
的指针的VLA(可变长度数组),而不是指向node
的指针的数组。这是导致错误的问题:
error: variably modified ‘table’ at file scope
(尚不清楚如何通过VLA使用gcc
进行编译。请参见C11 Standard - 6.7.6.2 Array declarators(p2))
相反,您需要#define
的值N
,使用全局enum
或将table
的声明移至块或函数作用域。 (请注意,N
的大小至少应为原来的十倍,以保持哈希冲突次数-并且哈希表 Load Factor [buckes_used/total_buckets
]低于{{1 }})
类型问题
您的哈希函数声明为:
0.7
它返回类型unsigned int hash(const char *word)
,您不应将结果分配给unsigned int
。虽然取模将使返回的值保持在正整数值的范围内,但是这很麻烦。
int
在其他情况下,如果第31位为// int hashnum = hash(lword);
unsigned int hashnum = hash(lword);
,则对1
的赋值将导致该值为负-如果用作数组索引,则会导致未定义行为。
int
函数比所需的函数复杂得多。您有一个全局数组unload()
用作哈希表。唯一需要释放的是存储桶中不为空的所有节点。因此,您只需要遍历每个存储桶并检查其是否为空。如果不是,则遍历以bucket元素开头的列表,以释放每个节点,例如:
table
不必要的代码
在很多地方,您的代码没错,但是涉及到额外的复制或对bool unload(void)
{
// for(size_t i=0; i<10000;i++) /* you have a constant -- use it */
for(size_t i=0; i<N;i++)
{
node *n = table[i];
while (n) {
node *victim = n;
n = n->next;
free (victim);
}
}
word_count = 0;
return true;
}
的不必要调用等。例如,在free()
中,无需分配给{{ 1}},只是在转换为小写字母后再次分配值。只需将其转换为小写并分配:
check()
在lword[i]
中,如果 for (size_t i = 0; i <= strlen(word); i++)
lword[i] = tolower(word[i]);
// {
// lword[i] = word[i];
// lword[i] = tolower(lword[i]);
// }
的分配失败,则不需要load()
,如先前所示。为什么?当new_node
失败时,它返回free(new_node);
。没有分配任何东西。尽管malloc()
无害(NULL
将对您进行检查),但这根本没有必要。
导致的内存使用
如果您进行了所有更改,则代码现在将运行而不会出现内存错误,并将释放分配的内存,例如
free(NULL);
我尚未检查所有答案的正确性,但是对于free()
,答案似乎正确。
仔细检查一下,如果还有其他问题,请告诉我。