c - 如何查看堆中有多少位置以及如何清除它?
问题描述
我怎么知道我在 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);
解决方案
如何检查我现在在 HEAP 中有多少位置?
别担心。
只需询问您需要什么并检查返回值。
ptr = calloc(N, sizeof *ptr);
if (!ptr) exit(EXIT_FAILURE);
//... use ptr ...
free(ptr);
推荐阅读
- regex - Google App Script findText 正则表达式不适用于换行符
- php - 通过 PHP 函数发送邮件
- php - 是否可以使用 POST 实现更新或删除功能,而无需在视图中使用 hidden()
- c# - Firefox 浏览器:不安全的证书 - Selenium c#
- r - R ANOVA 循环中的可变长度错误
- linux - 使用 bash 脚本删除旧备份
- excel - Assign values in large scale into a data base. Formula (libreOffice calc)
- node.js - nodejs如何做多个请求
- javascript - JavaScript 如何获取一个值并将其放入函数中
- python - 用不同的元素迭代列表