c - 释放循环链表

标签 c pointers linked-list

我有点了解如何释放它们,但我很确定我在代码中做错了。

while( *bestFriend != NULL){
                    temptr = *bestFriend;
                    *bestFriend = (*bestFriend)->next;
                    free(temptr);
                    printf("Freed\n");
                }

它使我的程序崩溃,但有点不确定是什么原因造成的。

编辑:其余代码

int duckDuckBoot(jimmysFriend **bestFriend, int rounds, int howManyDucks, int numberOfFriends, int gameCounter){

    int roundCounter;
    int i;
    jimmysFriend *temptr;
    temptr = *bestFriend;
    roundCounter = 0;
    if(rounds != 0){
     do{
        for(i = 0; i < howManyDucks;){
            i++;
            if(i == howManyDucks){
                    temptr = temptr->next;
                if((*bestFriend)->next == *bestFriend){
                    temptr = *bestFriend;
                    free(temptr);
                    *bestFriend = NULL;
                    printf("Game %d:\n", gameCounter);
                    printf("Jimmy has friends no more\n");
                    return 0;
                }
                else if(temptr->next == *bestFriend){
                 jimmysFriend *temptr2;
                 while(temptr->next->next != *bestFriend){
                        temptr = temptr->next;
                 }

                 temptr2 = temptr->next;
                 temptr->next = *bestFriend;
                 free(temptr2);
                 temptr = *bestFriend;
                }
                else if(temptr == *bestFriend){
                    jimmysFriend *temptr2;
                    temptr2 = *bestFriend;
                    while(temptr->next != *bestFriend){
                        temptr = temptr->next;
                    }
                    temptr->next = (*bestFriend)->next;
                    (*bestFriend) = (*bestFriend)->next;
                    free(temptr2);
                }
                else{
                    jimmysFriend* temptr2;
                    temptr2 = *bestFriend;

                    while(temptr2->next->next != temptr->next){
                        temptr2= temptr2->next;
                    }
                    jimmysFriend *temptr3;
                    temptr3 = temptr;
                    temptr2->next = temptr->next;
                    temptr = temptr->next;
                    temptr2 = NULL;
                    free(temptr3);
                    free(temptr2);


                }
                roundCounter++;
                }
            else{
            temptr = temptr->next;
            }


        }

        }while(roundCounter != rounds);
        if(roundCounter == rounds){
            char** nameList;
            int listSize;
            nameList = allocMemory(numberOfFriends);
            listSize = dataTransfer(*bestFriend, nameList, numberOfFriends);
            printf("Game %d:\n", gameCounter);
            for(i = 0; i < listSize; i++){
                printf("%s\n",nameList[i]);

                }

          for(i = 0; i < listSize; i++){
                    free(nameList[i]);
                    free(nameList);
                }
            while( *bestFriend != NULL){
                    temptr = *bestFriend;
                    *bestFriend = (*bestFriend)->next;
                    free(temptr);
                    printf("Freed\n");
                }


        }







    }


    return 1;
}

最佳答案

当你这样做时

while( *bestFriend != NULL)

你忘记了这是循环的。最后一个要释放的节点的下一个将是您释放的第一个节点。这会产生一个问题,因为该内存已从您的程序中释放。这将导致段错误。

我的建议是不要将列表放在循环庄园中,这没有什么区别,只是没有填充下一个指针。

关于c - 释放循环链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36168945/

相关文章:

android - 智能指针(Android强指针)引用类型拷贝

c - 使用多线程求多个链表遇到的最大键数

C++:如何做一个链表与结构作为参数传递?

c - 如何使程序能够在 Linux 上转储核心?

c - #define 然后 printf 不工作

C: 使用 printf 时出现段错误

java - 按名称对已排序的 LinkedList 进行排序

c - 如何将我的源代码添加到内核源代码树

c 读取字符串导致崩溃

c++ - `const T` 与 T = char* 不是 `const char*` 吗?