gpt4 book ai didi

c - CS50 Speller出现valgrind错误

转载 作者:行者123 更新时间:2023-11-30 20:30:17 27 4
gpt4 key购买 nike

我的代码可以编译并执行我想要的操作,但是valgrind总是吐出一点我不明白的错误。就是说我正在尝试使用未初始化的变量,但是它们已经非常清楚地初始化了。即使定义了变量,它也指出它尚未初始化。

很明显,我的代码可以正常工作,所以我不明白真正的问题是什么。我什至在网上看过别人的解决方案,我的代码似乎匹配。 valgrind只是讨厌我吗?到底是怎么回事?

我的代码:

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

#include "dictionary.h"

// Represents number of buckets in a hash table
#define N 26

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

// Represents a hash table, AKA an array of nodes
node *hashtable[N];

// Number of Words loaded into memory
int wordnum = 0;

// Initialize custom functions to be defined later
unsigned int hash(const char *word);
bool load(const char *dictionary);
unsigned int size(void);
bool check(const char *word);
bool unload(void);

// Hashes word to a number between 0 and 25, inclusive, based on its first letter
unsigned int hash(const char *word)
{
return tolower(word[0]) - 'a';
}

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

// Buffer for a word
char word[LENGTH + 1];

// Insert words into hash table
while (fscanf(file, "%s", word) != EOF)
{
int index = hash(word) % N;
if (hashtable[index] == NULL)
{
hashtable[index] = malloc(sizeof(node));
for (int i = 0, n = strlen(word); i < n; i ++)
{
hashtable[index]->word[i] = word[i];
}
hashtable[index]->next = NULL;
wordnum++;
}
else
{
node *new_node = malloc(sizeof(node));
for (int i = 0, n = strlen(word); i < n; i ++)
{
new_node->word[i] = word[i];
}
new_node->next = hashtable[index];
hashtable[index] = new_node;
wordnum++;
}
}

// Close dictionary
fclose(file);

// Indicate success
return true;
}

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

// Returns true if word is in dictionary else false
bool check(const char *word)
{
int index = hash(word);
node *current = hashtable[index];
while (current != NULL)
{
char tempword [LENGTH + 1];
strcpy(tempword, word);
for (int i = 0, n = strlen(tempword); i < n; i ++)
{
tempword[i] = tolower(tempword[i]);
}
if (strcmp(tempword, current->word) == 0)
{
return true;
}
current = current->next;
}
return false;
}

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

最佳答案

在加载中,当您找到要存储的单词时,便会初始化hashtable[index]。在卸载时,您尝试释放所有哈希表节点。如果字典中没有以“ z”开头的单词怎么办?然后,未初始化hashtable[25],并且valgrind将抱怨。

您是否知道CS50 has a stack forum关于CS50 pset的问题和答案很多?

关于c - CS50 Speller出现valgrind错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54598783/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com