首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我怎样才能理解来自val研输出的内存泄漏?

我怎样才能理解来自val研输出的内存泄漏?
EN

Stack Overflow用户
提问于 2021-07-21 18:59:35
回答 1查看 182关注 0票数 1

我正在上CS50课程,第五周我正在努力找出Pset5 Speller的答案。对于任何不熟悉的人,目标是编辑特定的.c文件以使五个函数正常运行,这样主函数(位于一个单独的文件中)可以执行以下操作:

将字典加载到哈希表中,将字典加载到哈希表中,通过此函数运行一个单词,以帮助将其加载到字典中,或者搜索单词的后继dictionary

  • CHECK dictionary

  • UNLOAD大小--查看文本中的单词是否在dictionary

  • UNLOAD中--释放字典,这样就不会有内存泄漏

请注意,这个文件是在类中给我的,我要编辑函数中的空间--我唯一能更改的是const unsigned int N = 1000;,我把它设置为1000,只是一个任意的数字,但它可以是任何东西。

我只在一件事上有困难(我能说出来)。为了使它运行,我已经做了一切,但是Check50 (告诉我是否正确的程序)告诉我我有内存错误:

代码语言:javascript
复制
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.

当我运行英勇时,这就是它给我的东西:

代码语言:javascript
复制
==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没有任何建议)。

这是我的实际代码(请记住,第二个文档中有一个主函数,它实际上利用了所提供的函数,因此可以,例如,函数似乎没有按正确的顺序排列)。

代码语言:javascript
复制
// 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;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-07-21 19:02:37

就像我们必须free我们malloc的每一个指针一样,我们必须fclose每一个我们fopenFILE*

你的问题来自于这条线:

代码语言:javascript
复制
FILE *file = fopen(dictionary, "r");

它没有相应的fclose(file)调用。在返回之前,将其添加到loads函数的末尾。

瓦兰可以提供非常有用的调试信息(特别是当您的代码是用-g编译以进行调试信息时),如下所示:

代码语言:javascript
复制
==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)

为您提供了堆栈跟踪,它分配了最终导致泄漏的内存--您可以看到,您自己的代码中的最后一行是dictionary.c:83,它是调用fopen的行。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68475131

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档