我如何知道我是否已成功释放整个单链表数组?
我创建了一个指针数组。我使用其中一些作为单链表的根。 lgroup
(指针数组)的大小为 10
,j
也可以达到 10
。我像这样分配每个 lptr
:
lgroup[i].lptr[j] = (node *) malloc (sizeof(node));
如果我不打算使用它,我设置 curr = lgroup[i].jptr[j];
和 curr ->下一个= NULL;
。 否则,我开始在每个 curr->next
上使用 malloc
,然后执行 curr = curr->next;
。当我不想向列表中添加更多节点时,我只需输入 curr->next = NULL;
并移至下一个 lptr
。相当标准的东西。
在程序结束时,我想释放
我为列表声明的所有内存。这就是我尝试这样做的方式:
for (i = 0; i < 10; i++) {
for (j = 0; j < 10; j++) {
if (lgroup[i].lptr[j] == NULL) {
free(lgroup[i].lptr[j]);
continue;
} else {
curr = lgroup[i].lptr[j];
while(curr->next != NULL) {
curr2 = curr->next;
free(curr);
curr = curr2;
}
}
}
}
问题是,经过大量的试验和错误以及大量“双重释放”和其他类似内容的消息,我最终得到了这段代码,所以我不完全确定是否它实际上释放了我所声明的所有内存,或者它恰好编译和运行时没有错误,但没有执行我想要的所有操作。我尝试在 gdb 上运行它,但通过查看内存地址我真的无法理解很多事情,所以我很好奇是否有一种方法可以检查它是否按我的预期工作,或者我是否注定要使用笔和纸并在我的脑海中运行代码。如果它确实达到了设计目的,是否有一种更简单、更干净的方法来达到相同的结果?
预先感谢您,如果您需要任何说明,请随时在评论中询问。
I have created an array of pointers. I have used some of them as roots for singly linked lists.
The size of lgroup
(the array of pointers) is 10
and j
can get up to 10
as well. I malloc every lptr
like this:
lgroup[i].lptr[j] = (node *) malloc (sizeof(node));
and if I'm not going to use it I set curr = lgroup[i].jptr[j];
and curr->next = NULL;
.
Otherwise I just start using malloc
on every curr->next
and then I do curr = curr->next;
. When I don't want to add any more nodes to a list I just put curr->next = NULL;
and move on to the next lptr
. Pretty standard stuff.
At the end of my program I want to free
all of the memory that I have claimed for my lists. This is how I try to do it:
for (i = 0; i < 10; i++) {
for (j = 0; j < 10; j++) {
if (lgroup[i].lptr[j] == NULL) {
free(lgroup[i].lptr[j]);
continue;
} else {
curr = lgroup[i].lptr[j];
while(curr->next != NULL) {
curr2 = curr->next;
free(curr);
curr = curr2;
}
}
}
}
The thing is that I ended up with this code after a lot of trial and error and a lot of messages of "double freeing" and other stuff like that, so I'm not completely sure if it actually frees all the memory that I have claimed or that it just happens to compile and run without error but not doing all the things I want it to. I tried to run it on gdb but I really can't understand a lot of things by looking at memory addresses so I was curious if there is a way to check if it works as I expected or if I'm doomed to do it using pen and paper and running the code in my head. If it happens to actually do what it was designed for, would there be an easier and cleaner way to achieve the same results?
Thank you in advance and if you need any clarification don't hesitate to ask for it in the comments.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 Valgrind 下运行您的代码。这是一个内存分析器,可以做很多事情,但它做的事情之一是在应用程序运行结束时检查未释放的内存。
Run your code under Valgrind. This is a memory profiler which does many things, but one of the things it does is check for unreleased memory at the end of an application run.
在您发布的代码中第一次调用
free
不会产生任何效果,因为参数是 NULL 指针(在该上下文中使用continue
也是不必要的)。当
lgroup[i].lptr[j]
不为 NULL 且lgroup[i].lptr[j]->next
时,您似乎也没有为lgroup[i].lptr[j]
调用 free > 为空。我会将其重写为:
The first call to
free
in the code you have posted does not have any effect, since the argument is a NULL pointer (usingcontinue
in that context is also unnecessary).It also seems you are not calling free for
lgroup[i].lptr[j]
when it is not NULL andlgroup[i].lptr[j]->next
is NULL.I would rewrite it to: