大家好,所以我正在做哈佛的CS50,我偶然发现了一个很有趣的问题,我一直无法解决。这个程序的想法是从一个巨大的单词列表中检查哪些单词拼写错误,并在最后释放内存。这些都是我没能纠正的错误。
:) dictionary.c, dictionary.h, and Makefile exist
:) speller compiles
:( handles most basic words properly
expected "MISSPELLED WOR...", not "MISSPELLED WOR..."
:) handles min length (1-char) words
:) handles max length (45-char) words
:) handles words with apostrophes properly
:) spell-checking is case-insensitive
:) handles substrings properly
这些都是具体的错误。
running ./speller basic/dict basic/text...
checking for output "MISSPELLED WORDS\n\n\nWORDS MISSPELLED: 0\nWORDS IN DICTIONARY: 8\nWORDS IN TEXT: 9\n"...
Expected Output:
MISSPELLED WORDS
WORDS MISSPELLED: 0
WORDS IN DICTIONARY: 8
WORDS IN TEXT: 9
Actual Output:
MISSPELLED WORDS
over
WORDS MISSPELLED: 1
WORDS IN DICTIONARY: 8
WORDS IN TEXT: 9
由于一些奇怪的原因,看似拼写错误的单词实际上是正确的。另一个错误如下。
valgrind tests failed; rerun with --log for more information.
Log
running valgrind --show-leak-kinds=all --xml=yes --xml-file=/tmp/tmpicmfiai5 -- ./speller substring/dict substring/text...
checking for output "MISSPELLED WORDS\n\nca\ncats\ncaterpill\ncaterpillars\n\nWORDS MISSPELLED: 4\nWORDS IN DICTIONARY: 2\nWORDS IN TEXT: 6\n"...
checking that program exited with status 0...
checking for valgrind errors...
112 bytes in 2 blocks are still reachable in defeat record 1 of 2: (file: dictionary.c, line: 76)
336 bytes in 6 blocks are definitely lost in defeat record 2 of 2: (file: dictionary.c, line: 31)
现在我将添加我的整个代码。
// Implements a dictionary's functionality
#include <strings.h>
#include <stdbool.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.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 = 27;
int word_count;
char dictionary_word[LENGTH + 1];
// Hash table
node *table[N];
// Returns true if word is in dictionary else false
bool check(const char *word)
{
// TODO
node *cursor = malloc(sizeof(node));
int word_pos = hash(word);
cursor = table[word_pos];
while (cursor != NULL)
{
if(strcasecmp (cursor->word, word) == 0)
{
return true;
}
cursor = cursor->next;
}
return false;
}
// Hashes word to a number
unsigned int hash(const char *word)
{
int index = 0 ;
for(int i = 0 ; word[i] != '\0' ; i++)
{
index += tolower(word[i]) ;
}
return index % N ;
}
// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
// open the file
FILE *file = fopen(dictionary, "r");
// check if file is null
if (!file)
{
fprintf(stderr, "File does not exist.\n");
return false;
}
char word[LENGTH + 1];
word_count = 0;
while(fscanf(file, "%s", dictionary_word) != EOF)
{
// allocate memory for the new node
node *new_node = malloc(sizeof(node));
if (new_node == NULL)
{
return false;
}
strcpy(new_node->word, dictionary_word);
int hashed = hash(dictionary_word);
if(table[hashed] == NULL)
{
table[hashed] = new_node;
new_node->next = NULL;
}
else
{
new_node->next = table[hashed];
table[hashed] = new_node->next;
}
word_count++;
}
fclose(file);
return true;
}
// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
//just return the word count, easy piesey.
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 = NULL;
while (cursor != NULL)
{
node *tmp = cursor;
cursor = cursor->next;
free(tmp);
}
}
return true;
}
非常感谢你,如果需要更多的信息,我会提供 只要我看到的消息。Thx.
我用你的代码运行了 small lalaland.txt
,有两个内存块分配在 dictionary.c::76
在退出前没有被释放的。 以下是错误信息。你可以使用这个 联系 来重现它。 对于所有由 malloc
,你需要打电话 free
一次。 您可以使用 printf
来找出哪些内存块没有被释放。最好用小字典和小文本输入来做这个调试。
Memory access warning: memory spaces are not freed; continue execution.
# 2 memory spaces are allocated at
# file:/dictionary.c::76, 26
# total 104 bytes
#