C 中 free() 的使用
我正在做一些 C 练习(新手),我遇到了一些问题。我得到了一个先进先出队列,并被告知将删除函数修改为 FILO。
当我省略 free((void *) p); 时,效果很好。 while 循环内的一行,我从之前的方法中获取了这一行。谁能告诉我为什么这条线存在时它不起作用?我猜由于内存泄漏我无法完全删除它?
/* remove next Item from queue, placing it in the 2nd argument;
* return 1/0 if successful/queue empty */
int q_remove(Queue *q, Item i) {
struct q_element *p;
if (q->head == NULL)
return 0;
if(q->head==q->tail){
p=q->head;
q->head=NULL;
q->tail=NULL;
memcpy(i, p->value, q->size);
free(p->value);
free((void *) p);
return 1;
}
p=q->head;
while(p != NULL){
if(p->next==q->tail){
memcpy(i, p->next->value, q->size);
free(p->next->value);
q->tail=p;
q->tail->next=NULL;
free((void *) p);
return 1;
}
p=p->next;
}
return 0;
}
I'm working through some C exercises (newbie) I hit a bit of a problem. I was given a first in first out queue and told to modify the remove function to be FILO.
This works fine when I leave out the free((void *) p); line inside the while loop, I took this line from the previous method. Can anyone tell me why it doesn't work when this line is there? I'm guessing I can't remove it entirly due to memory leaks?
/* remove next Item from queue, placing it in the 2nd argument;
* return 1/0 if successful/queue empty */
int q_remove(Queue *q, Item i) {
struct q_element *p;
if (q->head == NULL)
return 0;
if(q->head==q->tail){
p=q->head;
q->head=NULL;
q->tail=NULL;
memcpy(i, p->value, q->size);
free(p->value);
free((void *) p);
return 1;
}
p=q->head;
while(p != NULL){
if(p->next==q->tail){
memcpy(i, p->next->value, q->size);
free(p->next->value);
q->tail=p;
q->tail->next=NULL;
free((void *) p);
return 1;
}
p=p->next;
}
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题是您指向要在下一个代码行中释放的内容:
现在,当您尝试访问 q->tail 时,您会收到错误,因为您已经释放了该内存。
你应该尝试 free p->next 而不是 p (因为 p->next 是队列中的最后一项 - 所以这是你想要取出的)
祝你好运:)
the problem is that you point to something that you're going to free in the next code line:
now when you'll try to access q->tail you get an error since you've already freed this memory.
you should try free p->next instead of p (since p->next is the last item in your queue - so this is the one you want to take out)
good luck :)
这些行:
...应该可能是
因为您不想释放“p”(这是您的新“尾巴”),而是元素 p->next ,它是您的旧尾巴。释放指针后将其设置为 null。值 q->tail 已经指向 'p' 并且在此之后释放 'p' 无效,因为 'p->next' 是您要删除的内容。
可能就是这样。
另外,我个人不喜欢你在某处将“Item”类型定义为 void* 的方式,它看起来很混乱,因为“Item”看起来像堆栈上的按值传递变量。
凯夫
The lines:
...should probably be
because you're not trying to free 'p' (which is your new 'tail'), but the element p->next which was you're old tail. Set the pointer to null after you've free'd it. The value q->tail already points to 'p' and it isn't valid to free 'p' at after that, since 'p->next' is what you're trying to remove.
That might be it.
Also, I don't personally like how you've typedef'd 'Item' as a void* somewhere, it looks confusing because 'Item' looks like a passed-by-value variable on the stack.
Kev
在我看来,您正在释放
p
,但在此之前,您将p
的值分配给q->tail
留下q->tail
指向即将失效的内存。您实际上正在释放q->tail
。在其他地方,我怀疑您使用了
q->tail
,这会让您的操作系统尖叫并杀死您的程序,就像溜冰鞋上的哈迪斯一样。It seems to me you are freeing
p
, but before you do that you assign the value ofp
toq->tail
leavingq->tail
pointing to soon-to-be invalid memory. You are in effect freeingq->tail
.Somewhere else I suspect you use
q->tail
which will then make your OS scream and kill your program like Hades on rollerskates.