C语言 CS50 Pset5,在大字典上运行良好,但在小字典上存在 seg 错误



我在使用 CS50 课程的 PSET 5 时遇到了一些问题。这是拼写器问题。当我尝试加载小字典时,我的代码会出错。

使用 gdb 调试后,它显示当我尝试在第 83 行上 fopen(字典,"r"( 时发生错误。

bool load(const char *dictionary)
{
// fopen dictionary file (remb to check if NULL)
FILE *dict = fopen(dictionary, "r");
if (dict == NULL)
{
printf("Could not open filen");
return false;
}

它返回"无法打开文件"(。但是我不知道为什么会发生这种情况,有人可以帮忙吗?大字典的一切似乎都运行良好。

完整代码:

// Implements a dictionary's functionality
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.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 = 256;
// Hash table
node *table[N];
// Word_count for size function
unsigned long word_count = 0;
// Returns true if word is in dictionary else false
bool check(const char *word)
{
char small_word[LENGTH+1];
for (int i = 0; i < LENGTH + 1; i++)
{
small_word[i] = 0;
}

for (int i = 0, n = strlen(word); i < n; i++)
{
if(isupper(word[i]))
{
small_word[i] = tolower(word[i]);
}
else
{
small_word[i] = word[i];
}
}
// hash the word to get hash value
unsigned long hvalue = hash(small_word);
// point cursor to the first value in the table with index of hvalue
node* cursor = table[hvalue];
// as long as the strings don't match, we reassign cursor to the next value
while (strcasecmp(small_word, cursor->word) != 0)
{
cursor = cursor->next;
// since strings dont match, if they reach to the end where next is NULL
if (cursor == NULL)
{
// then the word is misspelled
return false;
}
}
return true;
}
// Hashes word to a number
unsigned long hash(const char *word)
{
// djb2 hash function as seen from http://www.cse.yorku.ca/~oz/hash.html
unsigned long hash = 0;
int c;
while ((c = *word++))
{
hash = c + (hash << 6) + (hash < 16) - hash;
}
return (hash % N);
}
// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
// fopen dictionary file (remb to check if NULL)
FILE *dict = fopen(dictionary, "r");
if (dict == NULL)
{
printf("Could not open filen");
return false;
}
char storage[LENGTH + 1];
for (int i = 0; i < LENGTH + 1; i++)
{
storage[i] = 0;
}
while (fscanf(dict, "%s", storage) != EOF)
{
// allocate space for buffer node
node *buffer = malloc(sizeof(node));
if (buffer == NULL)
{
printf("Error allocating memoryn");
return 1;
}
// copy the string in storage into the buffer node
strcpy(buffer->word, storage);
// hash the word to obtain hvalue
unsigned long hvalue = hash(buffer->word);

// assigns buffer's next to point to the current words inside the table
buffer->next = table[hvalue];

// after making sure that buffer next keeps the address of the other words, you assign the table hvalue to the most recent word
table[hvalue] = buffer;

// increase word count by one
word_count++;

}
fclose(dict);
return true;
}
// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned long size(void)
{
return word_count;
}
// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
word_count = 0;
for (int i = 0; i < N; i++)
{
node *cursor = table[i];
while (cursor != NULL)
{
node *tmp = cursor;
cursor = cursor->next;
free(tmp);
}

free(cursor);
}

return true;
}

"无法打开文件"消息可能是命令行上的拼写错误(如注释中所述,它不会产生 seg 错误,因为它提供消息,然后将false返回给拼写者,拼写器给出消息并退出(。

尝试 debug50,并将断点设置为node* cursor = table[hvalue];检查。使用texts/cat.txt作为文本文件。一旦你进入下一行,程序很可能会出错。

小字典(随发行版代码一起提供(不会填充table的所有成员。如果没有填充table[index],则没有table[index]->nexttable[index]->word这样的东西。因此,while (strcasecmp(small_word, cursor->word) != 0)是一个等待发生的赛格错误。

最新更新