【问题标题】:Heap not getting free in C堆在C中没有得到释放
【发布时间】:2023-03-13 09:17:01
【问题描述】:

这是我的代码 sn-p,我在尝试释放分配的内存时遇到错误。

int main()
{
    int value = 10; 
    totValues = pow(2, value);
    head = (node_t *)calloc(totValues, sizeof(node_t));
    createNode(head, 10, 20);
    free(head);
}

void createList(node_t *head, int tag, int index)
{
      node_t temp = (node_t) calloc(1, sizeof(sizeof(node_t)));
      temp->tag = tag;
      temp->next = head[index];
      head[index] = temp;
      free(temp);
 }

我正在尝试释放 head 和 temp 节点,但这不起作用并且我看到错误。我正在使用 valgrind 跟踪内存泄漏。不知道我做错了什么。

【问题讨论】:

  • 一方面,虽然你没有显示totValues 我怀疑calloc(sizeof(totValues), sizeof(node_t)); 应该是calloc(totValues, sizeof(node_t));
  • 你为什么要sizeof(sizeof(node_t))?将sizeof 运算符加倍不是您想要的。
  • 我更正了,但堆仍然被占用
  • 你不应该释放temp,因为你把它和head[index] = temp;放在了列表中。
  • 为什么要分配内存,用有用的信息填充它,然后释放它?!这有什么用?

标签: c struct heap-memory dynamic-memory-allocation


【解决方案1】:

您的示例中存在一些问题。以下是您的代码和一些修复:

#include <math.h>
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>

typedef struct node {
    int tag;
    struct node* next;
} node_t;

void createList(node_t**, int, int);

int main(int argc, char** argv)
{
    int value = 10; 
    int totValues = (int)pow(2, value);
    // `head` is an array of `node_t` pointers
    // (aka. it is a `node_t**` not a `node_t*`)
    node_t** head = (node_t**)calloc(totValues, sizeof(node_t*));
    createList(head, 10, 20);
    free(head);
    return 0;
}

void createList(node_t **head, int tag, int index)
{
    // You don't want `sizeof(sizeof(node_t))` because that is
    // equivalent to sizeof(size_t) which is implementation defined,
    // but usually equal to 4 or 8 depending on word size.
    node_t* temp = (node_t*)calloc(1, sizeof(node_t));
    temp->tag = tag;
    temp->next = head[index];
    head[index] = temp;
    // You fill `temp` with information, point a spot in `head` to
    // that pointer and then free the memory...
    // That makes no sense. Don't do it!
    free(temp);
}

请务必记住,free 不会递归地为双指针释放内存:您需要自己做。

您应该在head 中的每个节点上调用free,然后在head 本身上调用:

createList(head, 10, 20);
free(head[20]); //Free the one node we created
free(head); // Free the array of nodes we created

并确保注释掉或删除 createList 函数中的 free(temp); 调用。

当您对此运行 valgrind 时,不会再有内存泄漏。

==6322== Memcheck, a memory error detector
==6322== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==6322== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==6322== Command: ./dynamic_memory
==6322== 
==6322== 
==6322== HEAP SUMMARY:
==6322==     in use at exit: 0 bytes in 0 blocks
==6322==   total heap usage: 2 allocs, 2 frees, 8,208 bytes allocated
==6322== 
==6322== All heap blocks were freed -- no leaks are possible
==6322== 
==6322== For counts of detected and suppressed errors, rerun with: -v
==6322== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多