我一直遇到分段错误,我不确定为什么。我可能错误地使用了 strcasecmp 函数,但我不确定
I keep getting a segmentation fault and I'm not sure why. I may be using the strcasecmp function incorrectly but I'm not sure
我的代码可以编译,但是当我 运行 它时,我遇到了分段错误。我玩过 cs50 ide 中的调试工具,我的错误可能与使用 strcasecmp 函数有关,但我无法说出我做错了什么。任何建议将不胜感激。
// 实现字典的功能
#include <stdbool.h>
#include <strings.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.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 = 1000;
// Hash table
node *table[N];
// Returns true if word is in dictionary else false
bool check(const char *word)
{
node* temp = NULL;
int h = hash(word);
temp = table[h];
do
{
int result = strcasecmp(word, temp->word);
if (result == 0)
{
return true;
break;
}
else
{
temp = temp->next;
}
}
while (temp->next != NULL);
return false;
}
/* Adapted by Neel Mehta from
*/
// Hashes word to a number
unsigned int hash(const char *word)
{
unsigned long hash = 5381;
for (const char* ptr = word; *ptr != '[=10=]'; ptr++)
{
hash = ((hash << 5) + hash) + tolower(*ptr);
}
return hash % N;
}
// Loads dictionary into memory, returning true if successful else false
// create global count variable
int count;
bool load(const char *dictionary)
{
FILE *dict = fopen(dictionary, "r");
if (dict == NULL)
{
printf("Could not open dictionary.\n");
return false;
}
char temp[46];
count = 0;
// read one word at a time
while (fscanf(dict, "%s", temp)!= EOF)
{
node* n = malloc(sizeof(node));
if (n == NULL)
{
free(n);
return false;
}
else
{
int h = hash(temp);
strcpy(n->word, temp);
n->next = table[h];
table[h] = n;
count++;
}
}
fclose(dict);
return true;
}
// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
if (!count || count == 0)
{
return 0;
}
else
{
return count;
}
}
// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
node* temp = NULL;
for (int i = 0; i <= N; i++)
{
temp = table[i];
while (temp->next != NULL)
{
table[i] = temp->next;
free(temp);
temp = table[i];
}
}
return true;
}
当文本单词散列为空列表时,程序将在检查中出现段错误。当没有为特定索引填充 table 时,此 table->word
会产生段错误。 (任何对 table->next
的引用也会出现段错误)。请牢记这一点并查看 unload
。
我的代码可以编译,但是当我 运行 它时,我遇到了分段错误。我玩过 cs50 ide 中的调试工具,我的错误可能与使用 strcasecmp 函数有关,但我无法说出我做错了什么。任何建议将不胜感激。
// 实现字典的功能
#include <stdbool.h>
#include <strings.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.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 = 1000;
// Hash table
node *table[N];
// Returns true if word is in dictionary else false
bool check(const char *word)
{
node* temp = NULL;
int h = hash(word);
temp = table[h];
do
{
int result = strcasecmp(word, temp->word);
if (result == 0)
{
return true;
break;
}
else
{
temp = temp->next;
}
}
while (temp->next != NULL);
return false;
}
/* Adapted by Neel Mehta from
*/
// Hashes word to a number
unsigned int hash(const char *word)
{
unsigned long hash = 5381;
for (const char* ptr = word; *ptr != '[=10=]'; ptr++)
{
hash = ((hash << 5) + hash) + tolower(*ptr);
}
return hash % N;
}
// Loads dictionary into memory, returning true if successful else false
// create global count variable
int count;
bool load(const char *dictionary)
{
FILE *dict = fopen(dictionary, "r");
if (dict == NULL)
{
printf("Could not open dictionary.\n");
return false;
}
char temp[46];
count = 0;
// read one word at a time
while (fscanf(dict, "%s", temp)!= EOF)
{
node* n = malloc(sizeof(node));
if (n == NULL)
{
free(n);
return false;
}
else
{
int h = hash(temp);
strcpy(n->word, temp);
n->next = table[h];
table[h] = n;
count++;
}
}
fclose(dict);
return true;
}
// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
if (!count || count == 0)
{
return 0;
}
else
{
return count;
}
}
// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
node* temp = NULL;
for (int i = 0; i <= N; i++)
{
temp = table[i];
while (temp->next != NULL)
{
table[i] = temp->next;
free(temp);
temp = table[i];
}
}
return true;
}
当文本单词散列为空列表时,程序将在检查中出现段错误。当没有为特定索引填充 table 时,此 table->word
会产生段错误。 (任何对 table->next
的引用也会出现段错误)。请牢记这一点并查看 unload
。