首页 > 解决方案 > 如何查看堆中有多少位置以及如何清除它?

问题描述

我怎么知道我在 HEAP 中有多少位置?如果我有许多带有链表的代码已经运行并且没有使用 free() 函数,我该如何清理它?

例如这段代码我已经运行并且没有使用 free() 函数。我怎样才能清理那个函数做了什么,我怎样才能检查我现在在 HEAP 中有多少地方?

void main() {

int i, num,item;
LNODE* newNode;
LIST lst;
lst = makeEmptyList();
printf("Please enter the numbers of the nodes: ");
scanf("%d", &num);
printf("Please enter the value of the head: ");
scanf("%d", &item);
insertValueToHead(item, &lst);

for (i = 0; i < num-1; i++)
{
    printf("Please enter the value of the next node: ");
    scanf("%d", &item);
    newNode = createNewNode(item, lst.tail);
    AddToEndOfTheList(&lst, newNode);
}
printf("\n");
printList(&lst);

标签: cmemorylinked-listheap-memory

解决方案


如何检查我现在在 HEAP 中有多少位置?

别担心。

只需询问您需要什么并检查返回值。

ptr = calloc(N, sizeof *ptr);
if (!ptr) exit(EXIT_FAILURE);
//... use ptr ...
free(ptr);

推荐阅读