C 删除链表后释放内存
我在业余时间学习C。我熟悉C#、Java和Python。作为练习,我用C写了一个链表。它功能正确,有错误处理等 但是,我正在尝试修复内存泄漏。我知道C没有自动垃圾收集功能。那么,在删除列表成员后,如何“释放”它呢?我编写了一个名为removeAllList()的函数。该函数成功地从列表中删除了该成员,但我知道该成员的内存仍然被分配。我尝试过使用free([myArgument])函数,但它会导致无限循环。您能说明我将在哪里使用free()函数成功地为代码中删除的成员释放内存吗C 删除链表后释放内存,c,memory-management,linked-list,C,Memory Management,Linked List,我在业余时间学习C。我熟悉C#、Java和Python。作为练习,我用C写了一个链表。它功能正确,有错误处理等 但是,我正在尝试修复内存泄漏。我知道C没有自动垃圾收集功能。那么,在删除列表成员后,如何“释放”它呢?我编写了一个名为removeAllList()的函数。该函数成功地从列表中删除了该成员,但我知道该成员的内存仍然被分配。我尝试过使用free([myArgument])函数,但它会导致无限循环。您能说明我将在哪里使用free()函数成功地为代码中删除的成员释放内存吗 #include&
#include<stdio.h>
#include<stdlib.h>
struct Member{
int data;
struct Member *next;
};
struct List{
int size;
struct Member *root;
};
struct Member *createMember(int i){
struct Member *new;
new = malloc(sizeof(struct Member));
new->data = i;
new->next = NULL;
return new;
}
struct List *createList(int i){
struct List *new;
new = malloc(sizeof(struct List));
new->root = createMember(i);
new->size = 1;
return new;
}
void printList(struct List *list){
struct Member *current = list->root;
//error handling for empty list
if(list->size < 1){
printf("Error: List is empty");
}
//if list is not empty
else{
printf("List size: %i\nContents: ", list->size);
while(current->next != NULL){
printf("%i, ", current->data);
current = current->next;
}
printf("%i\n", current->data);
}
}
void addList(struct List *list, int i){
struct Member *current = list->root;
while(current->next != NULL){
current = current->next;
}
current->next = createMember(i);
list->size++;
}
void removeAllList(struct List *list, int i){
struct Member *current = list->root;
struct Member *prev = list->root;
if(list->size < 1){
//list is empty, end function now
return;
}
//remove all matching list head
while(current->data == i){
if(list->size <= 1){
list->root = NULL;
list->size--;
//list is empty, end function now
return;
}
else{
list->root = current->next;
current = list->root;
list->size--;
}
}
current = current->next;
//remove all matching list body
while(current->next != NULL && list->size > 1){
if(current->data == i){
prev->next = current->next;
list->size--;
}
prev = current;
current = current->next;
}
//remove all matching list tail
if(current->data == i && list->size > 1){
prev->next = NULL;
list->size--;
}
}
main(){
struct List *myList;
myList = createList(4);
addList(myList, 12);
addList(myList, 9);
addList(myList, 4);
addList(myList, 43);
addList(myList, 4);
printList(myList);
removeAllList(myList, 4);
printList(myList);
}
#包括
#包括
结构成员{
int数据;
结构成员*next;
};
结构列表{
整数大小;
结构成员*根;
};
结构成员*createMember(int i){
结构成员*new;
new=malloc(sizeof(结构成员));
新建->数据=i;
新建->下一步=空;
归还新的;
}
结构列表*创建列表(int i){
结构列表*新建;
new=malloc(sizeof(struct List));
新建->根=创建成员(i);
新建->大小=1;
归还新的;
}
作废打印列表(结构列表*列表){
结构成员*current=list->root;
//空列表的错误处理
如果(列表->大小<1){
printf(“错误:列表为空”);
}
//如果列表不是空的
否则{
printf(“列表大小:%i\n内容:”,列表->大小);
while(当前->下一步!=NULL){
printf(“%i”,当前->数据);
当前=当前->下一步;
}
printf(“%i\n”,当前->数据);
}
}
void addList(结构列表*列表,int i){
结构成员*current=list->root;
while(当前->下一步!=NULL){
当前=当前->下一步;
}
当前->下一步=创建成员(i);
列表->大小++;
}
void removeAllList(结构列表*列表,int i){
结构成员*current=list->root;
结构成员*prev=list->root;
如果(列表->大小<1){
//列表为空,现在结束函数
返回;
}
//删除所有匹配的列表头
而(当前->数据==i){
如果(列表->大小根=NULL;
列表->大小--;
//列表为空,现在结束函数
返回;
}
否则{
列表->根=当前->下一步;
当前=列表->根目录;
列表->大小--;
}
}
当前=当前->下一步;
//删除所有匹配的列表正文
while(当前->下一步!=NULL&&list->size>1){
如果(当前->数据==i){
上一个->下一个=当前->下一个;
列表->大小--;
}
prev=当前值;
当前=当前->下一步;
}
//删除所有匹配列表尾部
如果(当前->数据==i&&列表->大小>1){
prev->next=NULL;
列表->大小--;
}
}
main(){
结构列表*myList;
myList=createList(4);
地址列表(myList,12);
地址列表(myList,9);
地址列表(myList,4);
地址列表(myList,43);
地址列表(myList,4);
打印列表(myList);
removeAllList(myList,4);
打印列表(myList);
}
你不应该让占用内存的东西NULL
。你需要free()
像这样释放它们:free(mynode->next);
但是只有当你确定之前有malloc()
调用时
在你的代码中,你需要使用
free(list->root);
而不是list->root=NULL;
和free(prev->next);
而不是prev->next=NULL;
你试过man3free
和manmalloc
获取信息吗?