简体   繁体   中英

Why does my C Program crash when I try to delete a node out of a singe linked list

I am currently creating a Program in C which is basically a linked list inside of a linked list. The inner list being character and the outer list being words. Unfortunately I'm having Problems with deleting some of the outer Nodes (words) and freeing their memory. My program keeps crashing and I have no idea why. The compiler doesnt give me any warnings or errors and I've been looking for a fix for hours. Any help is apreciated for anyone who could look over the code! Thanks!

*void deleteWord (Node* Node, int index){
int counter = 0;
if (Node == NULL)
    return;

while (Node->next != NULL && counter != (index - 1)){
    Node = Node->next;
    counter++;
}
struct node* wordTemp = Node->next;
//Node->next = Node->next->next;

while (wordTemp->word != NULL){
    InnerNode* letterTemp = wordTemp->word->next;
    free(wordTemp->word);
    wordTemp->word = letterTemp;
}
free(wordTemp);
return;
}

Seems like you are freeing Node->next (stored in wordTemp), without re-assigning it, essentially breaking the link in the linked list, so now Node->next points to a deleted memory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM