[英]How can I understand a memory leak from valgrind output?
我正在參加 CS50 課程,我在第 5 周並試圖找出 Pset5 拼寫器。 對於不熟悉的人,目標是編輯特定的 .c 文件以使五個函數正常運行,以便主函數(位於單獨的文件中)可以執行以下操作:
請注意,該文件是在課堂上提供給我的,我要編輯函數中的空間 - 我唯一可以更改的另一件事是const unsigned int N = 1000;
我將其設置為 1000 作為一個任意數字,但它可以是任何數字。
我只有一件事(我可以說)有問題。 我已經做了一切讓它運行,但 Check50(判斷我是否正確執行的程序)告訴我我有內存錯誤:
Results for cs50/problems/2021/x/speller generated by check50 v3.3.0
:) dictionary.c exists
:) speller compiles
:) handles most basic words properly
:) handles min length (1-char) words
:) handles max length (45-char) words
:) handles words with apostrophes properly
:) spell-checking is case-insensitive
:) handles substrings properly
:( program is free of memory errors
valgrind tests failed; see log for more information.
當我運行 valgrind 這就是它給我的:
==347==
==347== HEAP SUMMARY:
==347== in use at exit: 472 bytes in 1 blocks
==347== total heap usage: 143,096 allocs, 143,095 frees, 8,023,256 bytes allocated
==347==
==347== 472 bytes in 1 blocks are still reachable in loss record 1 of 1
==347== at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==347== by 0x4A29AAD: __fopen_internal (iofopen.c:65)
==347== by 0x4A29AAD: fopen@@GLIBC_2.2.5 (iofopen.c:86)
==347== by 0x401B6E: load (dictionary.c:83)
==347== by 0x4012CE: main (speller.c:40)
==347==
==347== LEAK SUMMARY:
==347== definitely lost: 0 bytes in 0 blocks
==347== indirectly lost: 0 bytes in 0 blocks
==347== possibly lost: 0 bytes in 0 blocks
==347== still reachable: 472 bytes in 1 blocks
==347== suppressed: 0 bytes in 0 blocks
==347==
==347== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
這對我來說似乎很神秘,我希望有人可以幫助解釋並幫助我解決我的問題(Help50 沒有任何建議)。
這是我的實際代碼(請記住,還有一個帶有主要功能的第二個文檔,它實際使用了所提供的功能,因此可以,例如,這些功能似乎沒有按正確順序排列)。
// Implements a dictionary's functionality
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.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];
// Dictionary size
int dictionary_size = 0;
// Returns true if word is in dictionary, else false
bool check(const char *word)
{
// TODO #4!
// make lowercase copy of word
char copy[strlen(word) + 1];
for (int i = 0; word[i]; i++)
{
copy[i] = tolower(word[i]);
}
copy[strlen(word)] = '\0';
// get hash value
int h = hash(copy);
// use hash value to see if word is in bucket
if (table[h] != NULL)
{
node *temp = table[h];
while (temp != NULL)
{
if (strcmp(temp->word, copy) == 0)
{
return true;
}
temp = temp->next;
}
}
return false;
}
// Hashes word to a number
unsigned int hash(const char *word)
{
// TODO #2
// source: https://www.reddit.com/r/cs50/comments/1x6vc8/pset6_trie_vs_hashtable/cf9189q/
// I used this source because I had trouble understanding different variations - this one explained everything well.
// I modified it slightly to fit my needs
unsigned int h = 0;
for (int i = 0; i < strlen(word); i++)
{
h = (h << 2) ^ word[i];
}
return h % N;
}
// Loads dictionary into memory, returning true if successful, else false
bool load(const char *dictionary)
{
// TODO #1!
// open dictionary file
FILE *file = fopen(dictionary, "r");
if (file == NULL)
{
return false;
}
// read strings from file one at a time
char word[LENGTH + 1];
while (fscanf(file, "%s", word) != EOF)
{
node *n = malloc(sizeof(node));
if (n == NULL)
{
return false;
}
// place word into node
strcpy(n->word, word);
// use hash function to take string and return an index
int h = hash(word);
// make the current node point to the bucket we want
n->next = table[h];
// make the bucket start now with the current node
table[h] = n;
//count number of words loaded
dictionary_size++;
}
return true;
}
// Returns number of words in dictionary if loaded, else 0 if not yet loaded
unsigned int size(void)
{
// TODO #3!
return dictionary_size;
}
// Unloads dictionary from memory, returning true if successful, else false
bool unload(void)
{
// TODO #5!
for (int i = 0; i < N; i++)
{
while (table[i] != NULL)
{
node *temp = table[i]->next;
free(table[i]);
table[i] = temp;
}
}
return true;
}
就像我們必須free
malloc
每個指針一樣,我們必須fclose
我們fopen
每個FILE*
。
你的問題源於這一行:
FILE *file = fopen(dictionary, "r");
它沒有相應的fclose(file)
調用。 在返回之前,將此添加到loads
函數的末尾。
Valgrind 可以為調試提供非常有用的信息(尤其是當您的代碼使用-g
編譯以獲取調試信息時),例如您問題的摘錄:
==347== 472 bytes in 1 blocks are still reachable in loss record 1 of 1
==347== at 0x483B7F3: malloc (in /usr/lib/x86_64-linux-gnu/valgrind/vgpreload_memcheck-amd64-linux.so)
==347== by 0x4A29AAD: __fopen_internal (iofopen.c:65)
==347== by 0x4A29AAD: fopen@@GLIBC_2.2.5 (iofopen.c:86)
==347== by 0x401B6E: load (dictionary.c:83)
==347== by 0x4012CE: main (speller.c:40)
Valgrind 為您提供分配了最終泄漏的內存的堆棧跟蹤 - 您可以看到您自己代碼中的最后一行是dictionary.c:83
,它是調用 fopen 的行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.