CS50 PSET4无法在拼写检查器中释放内存

时间:2019-12-25 17:59:53

标签: c memory memory-leaks valgrind cs50

我的代码运行良好,但是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;
}

1 个答案:

答案 0 :(得分:0)

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

  

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