首页 > 解决方案 > cs50 pset5 拼写分割,记忆错误

问题描述

我正在做拼写程序,在过去的几天里,我觉得我在兜圈子,一遍又一遍地收到同样的问题。现在,错误是 free(): "invalid pointer Aborted",很可能是指我使用 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;
}

标签: ccs50

解决方案


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

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);

推荐阅读