这就是我运行help50 valgrind时得到的:
/etc/profile.d/cli.sh:第94行:538分段错误Valgrind。/spellertexts / cat.txt
看起来您的程序正在尝试访问不应访问的内存区域。您是否尝试过更改硬编码字符串中的字符?您是否正在访问超出数组大小的数组元素?您是否在取消引用尚未初始化的指针?您是否正在取消引用值为NULL的指针?释放指针后要取消引用吗?
我不知道自己在做什么错,非常感谢您的帮助。
这是speller.c的第94行:
91 // Ignore words with numbers (like MS Word can)
92 else if (isdigit(c))
93 {
94 // Consume remainder of alphanumeric string
95 while ((c = fgetc(file)) != EOF && isalnum(c));
96
97 // Prepare for new word
98 index = 0;
99 }
这是我的代码(dictionary.c):
// Implements a dictionary's functionality
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <strings.h>
#include "dictionary.h"
#define HASHTABLE_SIZE 1985
//word counter
int counter = 0 ;
// 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 = 26;
// Hash table
node *table[N];
// Returns true if word is in dictionary else false
bool check(const char *word)
{
int index = hash(word);
//Set to the head of the linked list
node* head = table[index];
if(head != NULL)
{
node* cursor = head;
while(cursor->next != NULL)
{
if(strcasecmp(cursor->word, word) == 0)
{
return true;
}
cursor = cursor->next;
}
}
return false;
}
//Hash table that I got from https://stackoverflow.com/questions/7666509/hash-function-for-string
// Hashes word to a number
unsigned long hash(const char *str)
{
unsigned long hash = 5381;
int c;
while ((c = *str++))
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
return hash;
}
/*unsigned int hash(const char *word)
{
// TODO
return 0;
}*/
// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
//Open dictionary for read
FILE *f = fopen(dictionary, "r");
//Check to see if dictionary is empty
if(f == NULL)
{
printf("Error opening the file.\n");
return false;
}
//array to store words
char words[LENGTH + 1];
//Read strings from the file
while(fscanf(f, "%s", words) != EOF)
{
//allocate enough memory to store a new node
node *n = malloc(sizeof(node));
//if it doesn't have enough memory to store a node
if(n == NULL)
{
unload();
return false;
}
//copy the word from the array to the node
strcpy(n->word, words);
//call hash function
int index = hash(words) % HASHTABLE_SIZE;
//insert the node in the hash table
n->next = table[index];
table[index] = n;
counter++;
}
//close the file
fclose(f);
//success
return true;
}
// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
// TODO
return counter;
}
// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
for(int i = 0 ; i < HASHTABLE_SIZE ; i ++)
{
node* cursor = table[i];
node* tmp = cursor;
while(cursor != NULL)
{
cursor = cursor->next;
free(tmp);
tmp = cursor;
}
free(cursor);
}
return true;
}
答案 0 :(得分:0)
三个明显的问题:
table
具有N
(26)个元素,如此处node *table[N];
load
将基于HASHTABLE_SIZE
在n->next = table[index];
处访问多达int index = hash(words) % HASHTABLE_SIZE;
(1985个)元素check
将访问多达? node* head = table[index];
中的元素(我确实知道哈希函数返回非常大的数字)