内存泄漏和内存问题

memory leak's and memory issues

提问人:Ali Esmaeili 提问时间:11/5/2023 最后编辑:Ali Esmaeili 更新时间:11/5/2023 访问量:14

问:

我在 CS50 第 5 周解决了这个问题,但 Valgrind 仍然对我的内存处理不满意,我不知道为什么。问题解决了,我只想知道这里出了什么问题。

注意该行: char *word = malloc(LENGTH + 1);负责 7 个错误中的 5 个,使用大小为 8 的未初始化值,如下所示: 未初始化的值是由堆分配创建的。

我会在 Valgrind 检测到代码中问题的行前面加上一条评论。

最后一种方法负责卸载内存。

typedef struct node
{
    char word[LENGTH + 1];
    struct node *next;
} node;

const unsigned int N = 26 * 26 * 26;

node *table[N];

unsigned int wnumber = 0;

bool check(const char *word)
{
    char uppered[LENGTH + 1];
    for (int i = 0, length = strlen(word); i < length; i++)
    {
        uppered[i] = toupper(word[i]);
    }
    uppered[strlen(word)] = '\0';

    int index = hash(uppered);
    if (table[index] != NULL)
    {
        node *tmpnode = table[index]->next;
        while (tmpnode != NULL)
        {
            if (strcasecmp(tmpnode->word, uppered) == 0)
            {
                return true;
            }
            tmpnode = tmpnode->next;
        }
    }
    return false;
}

unsigned int hash(const char *word)
{
    // Each word in each layer has 26 buckets
    int firstletter = word[0] - 'A' + 1;
    int secondletter = word[1] - 'A' + 1;
    int thirdletter = word[2] - 'A' + 1;
    return (26 * 26 * 26) - (firstletter * secondletter * thirdletter);
}

bool load(const char *dictionary)
{
    char *word = malloc(LENGTH + 1); //1 blocks are still reachable. // Error: Use of uninitialised value of size 8
    if (word == NULL)
    {
        printf("word in load method is NULL\n");
        return false;
    }
    FILE *inputdictionary = fopen(dictionary, "r"); //1 blocks are still reachable
    if (inputdictionary == NULL)
    {
        printf("dictionary didn't open and is NULL");
        return false;
    }
    else
    {

        while (fscanf(inputdictionary, "%s", word) != EOF) //1 blocks are still reachable
        {
            node *n = malloc(sizeof(struct node)); // 2,576 blocks are still reachable
            if (n == NULL)
            {
                printf("n node in load method is NULL\n");
                fclose(inputdictionary);
                free(word);
                return false;
            }

            for (int i = 0; i < strlen(word); i++)
            {
                word[i] = toupper(word[i]);
            }
            strcpy(n->word, word);

            int index = hash(word);

            if (table[index] == NULL)
            {
                table[index] = malloc(sizeof(node)); //65 blocks are still reachable
                if (table[index] == NULL) //Error: Invalid read of size 8
                {
                    printf("table[%i] is null", index);
                    fclose(inputdictionary);
                    free(word);
                    return false;
                }
            }

            if (table[index]->next == NULL) // Error: Use of uninitialised value of size 8
            {
                table[index]->next = n; //Error: Use of uninitialised value of size 8
            }
            else
            {
                n->next = table[index]->next;
                table[index]->next = n;
            }
            wnumber++;
        }
    }
    fclose(inputdictionary);
    free(word);
    return true;
}

unsigned int size(void)
{
    return wnumber;
}

bool unload(void)
{
    for (int i = 0; i < N; i++)
    {
        if (table[i] != NULL)
        {
            node *ptr = table[i]->next;
            while (ptr != NULL)
            {
                node *ptr1 = ptr->next;
                free(ptr);
                ptr = ptr1;
            }
            free(table[i]);
            table[i] = NULL;
        }
    }
    return true;
}

在我看来,unload() 根本不起作用,但我相信 CS50 检查机器人和与此文件相关的其他代码。

你一路走到这里是完全值得感激的。感谢您阅读所有内容并尝试提供帮助。

内存管理 泄漏 堆内存

评论


答: 暂无答案