如何查看堆中有多少空间,如何清除它?

问题描述 投票:0回答:1

我怎么知道我在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);
c memory linked-list heap clear
1个回答
1
投票

如何检查我现在在HEAP中有多少地方?

不用担心。

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

ptr = calloc(N, sizeof *ptr);
if (!ptr) exit(EXIT_FAILURE);
//... use ptr ...
free(ptr);
© www.soinside.com 2019 - 2024. All rights reserved.