C 中 free() 的使用

发布于 2024-11-02 06:05:00 字数 877 浏览 0 评论 0原文

我正在做一些 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

请帮我爱他 2024-11-09 06:05:00

问题是您指向要在下一个代码行中释放的内容:

q->tail=p;
...
free((void *) p);

现在,当您尝试访问 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:

q->tail=p;
...
free((void *) p);

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 :)

2024-11-09 06:05:00

这些行:

q->tail->next=NULL;
free((void *) p); 

...应该可能是

free( free( (void *) p->next );
p->next=NULL;

因为您不想释放“p”(这是您的新“尾巴”),而是元素 p->next ,它是您的旧尾巴。释放指针后将其设置为 null。值 q->tail 已经指向 'p' 并且在此之后释放 'p' 无效,因为 'p->next' 是您要删除的内容。

可能就是这样。

另外,我个人不喜欢你在某处将“Item”类型定义为 void* 的方式,它看起来很混乱,因为“Item”看起来像堆栈上的按值传递变量。

凯夫

The lines:

q->tail->next=NULL;
free((void *) p); 

...should probably be

free( free( (void *) p->next );
p->next=NULL;

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

偏闹i 2024-11-09 06:05:00

在我看来,您正在释放 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 of p to q->tail leaving q->tail pointing to soon-to-be invalid memory. You are in effect freeing q->tail.

Somewhere else I suspect you use q->tail which will then make your OS scream and kill your program like Hades on rollerskates.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文