尽管在程序中的某个时刻使用了 free(),但为什么我的程序在 C 中会泄漏内存?
Why is my program leaking memory in C despite using free() at some point in the program?
我的 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
unsigned int hash(const char *word)
{
unsigned long index = 5381;
for (const char *ptr = word; *ptr != '[=10=]'; 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个block中的56个字节仍然是可达的,具体参考:
new_node = malloc(sizeof(node));
位于加载函数中。我很困惑的是我最后释放了它:
free(new_node);
不过好像没有效果。这是我在能够提交并获得满分之前唯一剩下的问题,我想了解为什么那一行仍然存在内存泄漏。再次感谢!
两个问题:
new_node
不应该是全局的。它应该是 load
函数的局部。
释放内存时,不会释放每个链表中的最后一个元素。
运行 通过编译器发布的代码导致:
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.
那么它怎么可能给出 'run time' 问题呢?
编译时,始终启用警告,然后修复这些警告
已发布代码的逻辑也存在一些问题,但在进入逻辑之前,您确实需要让它干净地编译(和 link)。
调试逻辑最简单的方法之一是使用调试器,例如 gdb
或 IDE,例如 visual studio
。
我的 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
unsigned int hash(const char *word)
{
unsigned long index = 5381;
for (const char *ptr = word; *ptr != '[=10=]'; 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个block中的56个字节仍然是可达的,具体参考:
new_node = malloc(sizeof(node));
位于加载函数中。我很困惑的是我最后释放了它:
free(new_node);
不过好像没有效果。这是我在能够提交并获得满分之前唯一剩下的问题,我想了解为什么那一行仍然存在内存泄漏。再次感谢!
两个问题:
new_node
不应该是全局的。它应该是load
函数的局部。释放内存时,不会释放每个链表中的最后一个元素。
运行 通过编译器发布的代码导致:
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.
那么它怎么可能给出 'run time' 问题呢?
编译时,始终启用警告,然后修复这些警告
已发布代码的逻辑也存在一些问题,但在进入逻辑之前,您确实需要让它干净地编译(和 link)。
调试逻辑最简单的方法之一是使用调试器,例如 gdb
或 IDE,例如 visual studio
。