0

我在运行我的程序 glibc 时遇到此错误检测到* ./out: double free or corruption (!prev): 0x082a7dd0 尝试释放指针时出现问题,以下是部分代码:

//Structure to define an existing lightpath in the network
struct existing_lightpath{
    int* route;
    int wavelength;
    int* edges;
    int num;
    double LAR;
    double IAR;
    int LAR_group[100];
    int IAR_group[100];
    struct existing_lightpath* next_lightpath;
};

以及用于从链表中删除节点的代码

if(event_type_cur == 0 && existing_lightpath_list != NULL){
temp = existing_lightpath_list;
    previous = NULL;
    while(temp->num != lightpath_num_cur && temp->next_lightpath != NULL){
        previous = temp;
        temp = temp->next_lightpath;
}

next = temp->next_lightpath;
//delete the current node from the linked list
if(previous == NULL){
    if(next == NULL){
        free(temp->route);
        free(temp->edges);
        free(temp);
        temp = NULL;
        existing_lightpath_list = NULL;    
    }
    else {
        existing_lightpath_list = next;
        temp->next_lightpath = NULL;
        free(temp->route);
        free(temp->edges);
        free(temp);
    }
}
else{
    if(next == NULL){
        previous->next_lightpath = NULL;
        for(i=0; i<nodes && temp->route[i] != -1; i++)
            printf("%d ", temp->route[i]);
            free(temp->route);
            free(temp->edges);
            free(temp);
    }
    else{
        previous->next_lightpath = next;
        temp->next_lightpath = NULL;
        free(temp->route);
        free(temp->edges);
        free(temp);
    }
}

在第三种情况下检测到的错误:free(temp->route) 当上一个不为 NULL 但下一个在打印后为 NULL 时。我正在为程序中其他地方的链表节点分配内存,没有问题提前感谢您的帮助

4

0 回答 0