尽管在程序中的某个位置使用了free(),但是为什么我的程序仍在C中泄漏内存?

cjb.nyi

我的Speller程序运行正常,只是内存泄漏似乎无法解决。我已经满足了所有其他的check50要求,非常感谢有人对我的代码逻辑的帮助。非常感谢任何愿意花一些时间来帮助我的人!无论如何,这是代码:

// Implements a dictionary's functionality

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

#include "dictionary.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;

// Hash table
node *table[N];

// File pointer
FILE *read;

// Node pointer
node *new_node;

// Returns true if word is in dictionary else false
bool check(const char *word)
{
    // Input word into hash function
    int word_index = hash(word);

    // Check if word is in dictionary
    for (node *tmp = table[word_index]; tmp != NULL; tmp = tmp->next)
    {
        if (strcasecmp(word, tmp->word) == 0)
        {
            return true;
        }
    }
    return false;
}

// Hashes word to a number
// Credits to Neel Mehta from http://stackoverflow.com/questions/2571683/djb2-hash-function
unsigned int hash(const char *word)
{
    unsigned long index = 5381;

    for (const char *ptr = word; *ptr != '\0'; ptr++)
    {
        index = ((index << 5) + index) + tolower(*ptr);
    }

    return index % N;
}

// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
    // Initialize word
    char dict_word[LENGTH + 1];

    // Open file for reading
    read = fopen(dictionary, "r");
    if (read == NULL)
    {
        // Terminate program
        printf("Cannot open dictionary\n");
        return false;
    }

    // Loop until end of file
    while (fscanf(read, "%s", dict_word) != EOF)
    {
        // Initialize node pointer
        new_node = malloc(sizeof(node));
        if (new_node == NULL)
        {
            // Terminate program
            free(new_node);
            printf("Insufficient memory storage\n");
            return false;
        }

        // Copy word into node pointer
        strcpy(new_node->word, dict_word);

        // Set pointer to null
        new_node->next = NULL;

        // Call upon hash function
        int word_index = hash(new_node->word);

        // Index result into hash table
        if (table[word_index] == NULL)
        {
            // Open node
            table[word_index] = new_node;
        }
        else
        {
            // !Open node
            new_node->next = table[word_index];
            table[word_index] = new_node;
        }
    }
    return true;
}

// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
    // Iterate through the whole hash table
    int word_counter = 0;
    for (int i = 0; i < N; i++)
    {
        if (table[i] == NULL)
        {
            // Skip iteration
        }
        else
        {
            // Check how many words
            for (node *tmp = table[i]; tmp != NULL; tmp = tmp->next)
            {
                word_counter++;
            }
        }
    }
    return word_counter;
}

// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
    // Iterate through the hash function
    for (int i = 0; i < N; i++)
    {
        if (table[i] == NULL)
    {
        // Skip iteration
    }
        else
        {
            // Initialize pointers
            node *tmp_first = table[i];
            node *tmp_second = table[i]->next;

            // Iterate through the linked list
            while (tmp_second != NULL)
            {
                free(tmp_first);
                tmp_first = tmp_second;
                tmp_second = tmp_second->next;
            }
        }
    }
    free(new_node);
    fclose(read);
    return true;
}

Valgrind说,仍然可以在1块中达到56个字节,具体是指:

new_node = malloc(sizeof(node));

位于加载功能中。我感到困惑的是最后我释放了它:

free(new_node);

但似乎没有效果。这是我能够提交和获得完美分数之前剩下的唯一问题,我想理解为什么在那一行中仍然存在内存泄漏。再次感谢!

比尔·林奇

两个问题:

  1. new_node不应该是全球性的。它应该在load函数本地

  2. 释放内存时,您不会释放每个链接列表中的最后一个元素。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

程序中的C内存泄漏

为什么我的 swift 应用程序在 AVAsset 中“泄漏”内存

为什么我的C程序突然使用了30g的虚拟内存?

为什么我在EOutOfResources中泄漏内存?

尽管使用了更多线程,为什么我的程序变慢了?

C程序中绝对丢失的内存泄漏

为什么我的并行遍历Haskell程序会泄漏内存?

为什么我的程序会出现内存泄漏?

什么在我的程序中泄漏int数组?

mysqlcppconn程序中的内存泄漏

Haskell程序中的内存泄漏

为什么此程序打印带有奇怪字符的字符串?尽管释放内存泄漏?

在winxp中,我有4GB的RAM,正在使用1.77GB,但是程序会不断调出内存。为什么?

为什么字体家族在引导程序中处于两个位置?

我正在尝试使用free()释放结构中某个元素占用的内存,但是它不起作用

当我在飞行模式下启动应用程序时,为什么位置管理器在“ locationManager:didUpdateLocations:”中返回一个位置?

stdout是否存储在文件系统或内存中的某个位置?

无法找出C应用程序中的内存泄漏

长期运行的C ++应用程序中的内存泄漏

远程识别客户使用的应用程序中的内存泄漏

为什么这不是C ++中的内存泄漏?

我的程序随着时间的推移而变慢,我不知道为什么。内存泄漏?

为什么在我的程序中两次调用了operator()?

为什么我的if块无法在我的C程序中执行?

简单金属程序中的内存泄漏

Spark驱动程序中的内存泄漏

当我使用free()时,C中的程序随机崩溃

当我在Thread对象上调用run()时,为什么Java程序会泄漏内存?

为什么数据在同一物理内存中的8086中的多个位置存在?