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

问题描述

我的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. 释放内存时,您不会释放每个链接列表中的最后一个元素。

,

通过编译器运行发布的代码会导致:

gcc -ggdb3 -Wall -Wextra -Wconversion -pedantic -std=gnu11 -c "untitled1.c" -o "untitled1.o" 

untitled1.c:24:7: error: variably modified ‘table’ at file scope
   24 | node *table[N];
      |       ^~~~~

untitled1.c: In function ‘check’:
untitled1.c:36:22: warning: implicit declaration of function ‘hash’ [-Wimplicit-function-declaration]
   36 |     int word_index = hash(word);
      |                      ^~~~

untitled1.c: At top level:
untitled1.c:51:14: error: conflicting types for ‘hash’
   51 | unsigned int hash(const char *word)
      |              ^~~~

untitled1.c:36:22: note: previous implicit declaration of ‘hash’ was here
   36 |     int word_index = hash(word);
      |                      ^~~~

untitled1.c: In function ‘hash’:
untitled1.c:57:40: warning: conversion to ‘long unsigned int’ from ‘int’ may change the sign of the result [-Wsign-conversion]
   57 |         index = ((index << 5) + index) + tolower(*ptr);
      |                                        ^

untitled1.c:60:18: warning: conversion from ‘long unsigned int’ to ‘unsigned int’ may change value [-Wconversion]
   60 |     return index % N;
      |            ~~~~~~^~~

untitled1.c: In function ‘load’:
untitled1.c:98:26: warning: conversion to ‘int’ from ‘unsigned int’ may change the sign of the result [-Wsign-conversion]
   98 |         int word_index = hash(new_node->word);
      |                          ^~~~

untitled1.c: In function ‘size’:
untitled1.c:121:23: warning: comparison of integer expressions of different signedness: ‘int’ and ‘unsigned int’ [-Wsign-compare]
  121 |     for (int i = 0; i < N; i++)
      |                       ^

untitled1.c:136:12: warning: conversion to ‘unsigned int’ from ‘int’ may change the sign of the result [-Wsign-conversion]
  136 |     return word_counter;
      |            ^~~~~~~~~~~~

untitled1.c: In function ‘unload’:
untitled1.c:143:23: warning: comparison of integer expressions of different signedness: ‘int’ and ‘unsigned int’ [-Wsign-compare]
  143 |     for (int i = 0; i < N; i++)
      |                       ^

Compilation failed.

那么怎么可能会出现“运行时”问题?

编译时,请始终启用警告,然后修复这些警告

发布的代码的逻辑也有几个问题,但是在进入逻辑之前,您确实需要对其进行干净地编译(和链接)。

调试逻辑的最简单方法之一是使用调试器(例如gdb或IDE(例如visual studio)。