这个错误是什么意思:"free(): double free detected in tcache 2"

what does this error means : "free(): double free detected in tcache 2"

#include <stdlib.h>
#include <stdio.h>

struct node {
    int value;
    struct node* next;
};

typedef struct node node_t;


void print_list(node_t *head) {
    node_t *temp = head;

    while(temp -> next != NULL) {
        printf("%d -> ", temp->value);
        temp = temp->next;
    }
    printf("%d ", temp->value);
    printf("\n");
}

node_t* create_new_node(int value) {
    node_t *temp;
    temp = malloc(sizeof(node_t));
    temp -> value = value;

    return temp;
}

void insert_after_node(node_t *tmp, node_t *newnode) {
    newnode -> next = tmp -> next;
    tmp -> next = newnode;
}

node_t* find_node(int num, node_t* head) {
    node_t *tmp = head;
    while(tmp != NULL) {
        if(tmp -> value == num)return tmp;
        tmp = tmp -> next;
    }

    return NULL;
}

node_t* delete_head(node_t* head) {
    node_t* temp = head;
    head = head -> next;
    free(temp);

    return head;
}

void free_list(node_t* head) {
    node_t* temp = head;
    while(head != NULL) {
        free(temp);
        temp = head;
        head = head -> next;
    }
}

int main(){
    node_t *head = NULL;
    node_t *temp = NULL;
    for(int i = 0; i < 10; i++) {
        temp = create_new_node(i);
        temp -> next = head;
        head = temp;
    }

    print_list(head);
    insert_after_node(
        find_node(8, head),
        create_new_node(13));
    print_list(head);

    head = delete_head(head);
    print_list(head);


    free_list(head);
    return 0;
}

输出:

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)

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

函数内free_list

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);
    }
}

虽然像这样定义函数会更好

void free_list(node_t **head) {
    while( *head != NULL ) {
        node_t* temp = *head;
        *head = ( *head ) -> next;
        free(temp);
    }
}

并且该函数被调用为

free_list( &head );

在这种情况下,调用函数后 main 中的指针 head 将等于 NULL

void free_list(node_t* head) {
    node_t* temp = NULL;
    while(head != NULL) {
        temp = head;
        head = head -> next;
        free(temp);
    }
}

需要更改 free_list(node_t* head) 函数。我尝试释放头部两次而不将其更改为下一个。