【发布时间】:2020-06-19 10:16:50
【问题描述】:
任何人都可以帮助/指导我找到这段代码有内存泄漏的地方吗?
这是代码
// Implements a dictionary's functionality
#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 = 100;
// Hash table
node *table[N];
// Counter for size
int counter;
// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
FILE *file = fopen(dictionary, "r");
char w[LENGTH + 1];
// Check if memory is available
if (file == NULL)
return false;
while(fscanf(file, "%s", w) != EOF)
{
node *n = malloc(sizeof(node));
// Check if memory is available
if (n == NULL)
return false;
n->next = NULL;
strcpy(n->word, w);
int pos = hash(n->word);
node *h = malloc(sizeof(node));
if (h == NULL)
table[pos] = n;
else
{
n->next = table[pos];
table[pos] = n;
}
counter++;
}
size();
fclose(file);
return true;
}
// Hashes word to a number
unsigned int hash(const char *word)
{
int prod = 1;
int len = strlen(word);
for (int i = 0; i < len; i++)
{
prod *= (int) word[i];
}
return prod % N;
}
// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
return counter;
}
// Returns true if word is in dictionary else false
bool check(const char *word)
{
int h = hash(word);
node *ptr = malloc(sizeof(node));
if (ptr == NULL)
return false;
ptr = table[h];
while (true)
{
if (ptr == NULL)
break;
bool b = (strcasecmp(ptr->word, word)) == 0;
if (b == true)
{
free(ptr);
return b;
}
ptr = ptr->next;
}
free(ptr);
return false;
}
// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
for (int i = 0; i < N; i++)
{
node *ptr = malloc(sizeof(node));
node *tmp = malloc(sizeof(node));
if (ptr || tmp == NULL)
return 1;
ptr = table[i];
// Free each linked list
while (tmp != NULL)
{
tmp = ptr;
ptr = ptr->next;
free(tmp);
}
free(ptr);
}
return false;
}
这是 help50 valgrind 消息
寻求帮助...
==16020== 56 bytes in 1 blocks are definitely lost in loss record 1 of 7
==16020== at 0x4C2FB0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==16020== by 0x401302: unload (dictionary.c:109)
==16020== by 0x400E09: main (speller.c:152)
看起来您的程序泄漏了 56 字节的内存。您是否忘记释放通过 malloc 分配的内存?仔细看看dictionary.c的第109行。
【问题讨论】:
-
node *h = malloc(sizeof(node));看起来是个问题 -
您从不以任何方式使用
h,除了检查分配是否成功。 -
哦,是的,谢谢!我已经摆脱了它,但它仍然向我显示同样的错误
标签: c memory-leaks cs50