Free a Circular Linked List

I kind of understand how to free them but I'm pretty sure I am doing it wrong in my code.

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

its crashing my program a bit unsure what is causing it though.

Edit: rest of Code

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

2 Answers

When you do

while( *bestFriend != NULL)

You forget that this is circular. The next of the last node to be freed will be the first node you freed. This creates a problem since that memory was deallocated from your program. This will cause a segmentation fault.

My suggestion is to not have the list in a circular manor it will make no difference just one next pointer is not filled.

Circular linked list will never point to NULL, given it has one or more node.

But you are doing while( *bestFriend != NULL) which means that you are not treating the given list as circular.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Sophia Al-Mansoor

Sophia Al-Mansoor

Global Business & E-Commerce Reporter

Sophia analyzes international trade, startup ecosystems, retail transformation, and supply chain logistics for modern digital publications.

Share this article
Twitter Facebook Pinterest