我的代码运行完美,但是valgrind显示分配给所有节点的内存仍然可以访问。这将导致check50的内存泄漏测试失败。
这是valgrind显示的内容-

堆摘要:
== 14338 ==在出口处使用:143,091个块中的8,013,096字节
== 14338 ==总堆使用量:143,096个分配,5个空闲,8,023,416字节已分配
            丢失记录1 of 1中仍然可以达到143,091块中的8,013,096字节

这是我的代码-

#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>

#include "dictionary.h"

// Represents number of buckets in a hash table
#define N 26

// Represents a node in a hash table
typedef struct node
{
    char word[LENGTH + 1];
    struct node *next;
}
node;

// Represents a hash table
node *hashtable[N];

//to count no. of words in the dictionary
int count = 0;

// Hashes word to a number between 0 and 25, inclusive, based on its first letter
unsigned int hash(const char *word)
{
    return tolower(word[0]) - 'a';
}

// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
    // Initialize hash table
    for (int i = 0; i < N; i++)
    {
        hashtable[i] = NULL;
    }

    // Open dictionary
    FILE *file = fopen(dictionary, "r");
    if (file == NULL)
    {
        unload();
        return false;
    }

    // Buffer for a word
    char word[LENGTH + 1];

    // Insert words into hash table
    while (fscanf(file, "%s", word) != EOF)
    {
        // TODO

        //create a new node
        node *newnode = (node *)malloc(sizeof(node));

        //check if new node is allocated memory successsfully
        if (newnode == NULL)
        {
            unload();
            return false;
        }

        //copy word from dictionary to new node
        //newnode->word = word; (X) why?
        strcpy(newnode->word, word);

        //hash the word
        int n = hash(word);

        //add node to the correct bucket
        newnode->next = hashtable[n];
        hashtable[n] = newnode;

        count++;

    }

    // Close dictionary
    fclose(file);

    // Indicate success
    return true;
}

// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
    // TODO
    return count;
}

// Returns true if word is in dictionary else false
bool check(const char *word)
{
    // TODO

    //hash the word to find its bucket
    int n = hash(word);

    //traverse through the bucket
    node *temp = hashtable[n];
    while (temp != NULL)
    {
        if (strcasecmp(temp->word, word) == 0)
        {
            return true;
        }
        temp = temp->next;
    }

    return false;
}

// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
    // TODO

    node *cursor;

    for (int i = 0; i > 26; i++)
    {
        cursor = hashtable[i];
        while (cursor != NULL)
        {
            node *temp = cursor;
            cursor = cursor->next;
            free(temp);
        }
    }

    return true;
}

最佳答案

使用debug50在此行for (int i = 0; i > 26; i++)处的断点进行卸载并逐步执行。刚刚发生了什么???一行中有一个错字。将鼠标悬停在下面以扰流板。


   由于i初始化为0并且i > 26为false,因此循环永远不会处理

关于c - CS50 PSET4无法在拼写检查器中释放内存,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59480758/

10-10 14:51