Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
这个错误的意思是什么;free():在tcache 2中检测到双重空闲;_C_Pointers_Linked List_Free_Singly Linked List - Fatal编程技术网

这个错误的意思是什么;free():在tcache 2中检测到双重空闲;

这个错误的意思是什么;free():在tcache 2中检测到双重空闲;,c,pointers,linked-list,free,singly-linked-list,C,Pointers,Linked List,Free,Singly Linked List,我试图用C实现链表的基本操作。它有一些基本功能,例如使用malloc创建新节点、打印列表、在特定节点后插入节点、使用free释放整个列表。但是我犯了错误。有谁能告诉我这个错误可能意味着什么。在函数免费列表中 9 -> 8 -> 7 -> 6 -> 5 -> 4 -> 3 -> 2 -> 1 -> 0 9 -> 8 -> 13 -> 7 -> 6 -> 5 -> 4 -> 3 -> 2 ->

我试图用C实现链表的基本操作。它有一些基本功能,例如使用malloc创建新节点、打印列表、在特定节点后插入节点、使用free释放整个列表。但是我犯了错误。有谁能告诉我这个错误可能意味着什么。

在函数
免费列表中

9 -> 8 -> 7 -> 6 -> 5 -> 4 -> 3 -> 2 -> 1 -> 0 
9 -> 8 -> 13 -> 7 -> 6 -> 5 -> 4 -> 3 -> 2 -> 1 -> 0 
8 -> 13 -> 7 -> 6 -> 5 -> 4 -> 3 -> 2 -> 1 -> 0 
free(): double free detected in tcache 2
Aborted (core dumped)
您将删除指向头部节点的指针指向的内存两次。 在循环的第一次迭代中,您将删除指向head节点的指针所指向的内存

void free_list(node_t* head) {
    node_t* temp = head;
    while(head != NULL) {
        free(temp);
        temp = head;
        head = head -> next;
    }
}
在循环的第二次迭代中,由于这个赋值,你也在做同样的事情

    node_t* temp = head;
    while(head != NULL) {
        free(temp);
        //...
此外,这项声明

temp = head;
调用未定义的行为,因为使用了指向已释放内存的指针

函数应至少按照以下方式定义

head = head -> next;
虽然最好像这样定义函数

void free_list(node_t* head) {
    while(head != NULL) {
        node_t* temp = head;
        head = head -> next;
        free(temp);
    }
}
这个函数被称为like

void free_list(node_t **head) {
    while( *head != NULL ) {
        node_t* temp = *head;
        *head = ( *head ) -> next;
        free(temp);
    }
}
在这种情况下,调用函数后,
main
中的指针
head
将等于
NULL

free_list( &head );

自由列表(节点头)功能中所需的更改。我尝试了两次释放head,但没有将其更改为next。

您将同一指针传递到
free
两次。现在是学习如何调试程序的好时机。例如,我建议您在监视变量及其值(特别是跟踪所有指针指向的位置)的同时,逐条查看
free_列表
函数语句。感谢您解决了free_列表中的问题@某个程序员
free_list( &head );
void free_list(node_t* head) {
    node_t* temp = NULL;
    while(head != NULL) {
        temp = head;
        head = head -> next;
        free(temp);
    }
}