gpt4 book ai didi

cs50 pset5 拼写分割,内存错误

转载 作者:行者123 更新时间:2023-12-05 04:47:55 25 4
gpt4 key购买 nike

我正在做拼写程序,在过去的几天里,我觉得自己在兜圈子,一遍又一遍地遇到同样的问题。现在,错误是 free(): "invalid pointer已中止”,很可能指的是哈希函数,我在其中使用了 calloc,但我不明白我在那里做错了什么。很可能我的其他函数中存在多个错误,因此非常感谢有关它们的提示。我将发布整个程序,这样我就不必稍后发送它的片段了。完全迷失在这里。好的,感谢 Tim Randal 的回答,free() 错误看起来已修复,但是之前提到的段错误代替了他。

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dictionary.h"
#include <strings.h>
#include <ctype.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;

// Number of words loaded into the dictionary
int counter = 0;
// Hash table
node *table[N] = {0};

// Returns true if word is in dictionary, else false
bool check(const char *word)
{
size_t len = strlen(word);
char *lower = calloc(len+1, sizeof(char));

for (size_t i = 0; i < len; ++i) {
lower[i] = tolower((unsigned char)word[i]);
}
node *cursor = NULL;
cursor = table[hash(lower)];
while (cursor != NULL)
{
if (strcasecmp(cursor->word,lower) == 0)
{
free(lower);
return true;
}
else
{
cursor = cursor->next;
}
}
free(lower);
return false;
}

// Hashes word to a number
//hash function djb2 by Dan Bernstein
unsigned int hash(const char *word)
{
size_t len = strlen(word);
char *lower = calloc(len+1, sizeof(char));

for (size_t i = 0; i < len; ++i)
{
lower[i] = tolower((unsigned char)word[i]);
}
unsigned long hash = 5381;
int c;

while ((c = *lower++))
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
free(lower);
if (hash > N)
{
hash = hash % N;
return hash;
}
else
{
return hash;
}

}

// Loads dictionary into memory, returning true if successful, else false
bool load(const char *dictionary)
{
char word[LENGTH + 1];
node *new_node = NULL;
node *tmp = NULL;
FILE *file = fopen(dictionary,"r");
if (file == NULL)
{
printf("Could not open file\n");
return false;
}
while(fscanf(file, "%s",word) != EOF)
{
new_node = malloc(sizeof(node));
if (new_node == NULL)
{
printf("Not enough memory!\n");
return false;
}
strcpy(new_node->word,word);
unsigned int index = hash(word);

if(table[index] == NULL)
{
table[index] = new_node;
new_node->next = NULL;
}
else
{
tmp = table[index];
table[index] = new_node;
new_node->next = tmp;
}
counter++;



}
return true;
}

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

return counter;
}

// Unloads dictionary from memory, returning true if successful, else false
bool unload(void)
{
const char *word = NULL;
int index = hash(word);
for(node *cursor = table[index]; cursor != NULL;cursor = cursor->next)
{
for(node *tmp = table[index]; tmp != NULL; tmp = tmp->next)
{
free(tmp);
}
free(cursor);
}
return true;
}

最佳答案

这是一个问题。我并不是说它是唯一的。

while ((c = *lower++))
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
free(lower);

您在更改指针后释放指针。您需要将 lower 复制到另一个指针,该指针会递增并进行测试。原来的lower需要记住分配的第一个字节的地址。

char* test = lower;
while ((c = *test++))
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
free(lower);

关于cs50 pset5 拼写分割,内存错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68290569/

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