为什么使用 free() 会导致无限循环
Why does using free() lead to infinite looping
当我运行下面的代码时,它给了我一个无限循环的结果。但是,如果我注释掉 insert
函数中的自由指针行,即 free(ptr)
和 free(ptrnext)
那么它就可以正常工作。谁能解释一下为什么会这样?
我很确定 print 和 takeInput 工作正常,因此可以忽略。
#include<stdio.h>
#include<stdlib.h>
typedef struct Nodes{
struct Nodes * next;
int val;
}Node;
//Function to create a linked list
Node * takeInput(){
int data;
Node *start =NULL ;
Node *tail=NULL;
printf("Enter the number of nodes");
int num,i;
scanf("%d",&num);
for(i=1;i<=num;i++){
if(start==NULL){
start=malloc(sizeof(Node));
puts("Enter data");
scanf("%d",&data);
start->val=data;
start->next=NULL;
tail=start;
}
else{
Node * ptr = malloc(sizeof(Node));
puts("Enter data" );
scanf("%d",&data);
ptr->val=data;
tail->next=ptr;
tail=tail->next;
}
}
tail->next=NULL;
return start;
}
//Function to print
void print(Node * head){
Node*ptr=head;
while(ptr!=NULL){
printf("%d->",ptr->val);
ptr=ptr->next;
}
}
//Function to insert a node in given linked list
Node * insert(Node *start){
int i,data;
puts("Enter pos");
scanf("%d",&i);
puts("Enter data");
scanf("%d",&data);
Node * ptr=malloc(sizeof(Node));
ptr->val=data;
ptr->next=NULL;
if(i==1){
ptr->next=start;
start=ptr;
free(ptr);
}
else{
Node * ptrnext=start;
while(i!=1){
ptrnext=ptrnext->next;
i--;
}
ptr->next=ptrnext->next;
ptrnext->next=ptr;
free(ptr);
free(ptrnext);
}
return start;
}
int main(void){
Node * start =takeInput();
start=insert(start);
print(start);
}
When I run the following code, it gives me an infinite looping result. However if I comment out the free pointer lines in the insert function i.e. free(ptr)
and free(ptrnext)
then it works fine.
这是未定义的行为。 (当您不注释 free()
函数时)
释放内存后,请记住不要再使用它。
Note : the pointer might or might not point the same block after freeing, it's undefined behavior
所以不要释放指针,除非你想 destroy
或 delete
节点。
所以不要在 insert
函数中使用 free()
,因为您没有删除任何节点。
除此之外,我没有看到任何函数可以在程序结束时释放内存。
始终确保在最后使用 delete()
函数 释放 分配的内存 。
这是delete
函数的典型实现
void delete(Node* start)
{
Node* temporary = NULL;
while(start != NULL)
{
temporary = start->next; //saving next node address
free(start); //freeing current node
start = temporary; //assigning start with next node address
}
printf("successfully destroyed the list!"); //function exit message
}
在 main()
函数末尾或当您希望 delete
整个列表时调用它
当我运行下面的代码时,它给了我一个无限循环的结果。但是,如果我注释掉 insert
函数中的自由指针行,即 free(ptr)
和 free(ptrnext)
那么它就可以正常工作。谁能解释一下为什么会这样?
我很确定 print 和 takeInput 工作正常,因此可以忽略。
#include<stdio.h>
#include<stdlib.h>
typedef struct Nodes{
struct Nodes * next;
int val;
}Node;
//Function to create a linked list
Node * takeInput(){
int data;
Node *start =NULL ;
Node *tail=NULL;
printf("Enter the number of nodes");
int num,i;
scanf("%d",&num);
for(i=1;i<=num;i++){
if(start==NULL){
start=malloc(sizeof(Node));
puts("Enter data");
scanf("%d",&data);
start->val=data;
start->next=NULL;
tail=start;
}
else{
Node * ptr = malloc(sizeof(Node));
puts("Enter data" );
scanf("%d",&data);
ptr->val=data;
tail->next=ptr;
tail=tail->next;
}
}
tail->next=NULL;
return start;
}
//Function to print
void print(Node * head){
Node*ptr=head;
while(ptr!=NULL){
printf("%d->",ptr->val);
ptr=ptr->next;
}
}
//Function to insert a node in given linked list
Node * insert(Node *start){
int i,data;
puts("Enter pos");
scanf("%d",&i);
puts("Enter data");
scanf("%d",&data);
Node * ptr=malloc(sizeof(Node));
ptr->val=data;
ptr->next=NULL;
if(i==1){
ptr->next=start;
start=ptr;
free(ptr);
}
else{
Node * ptrnext=start;
while(i!=1){
ptrnext=ptrnext->next;
i--;
}
ptr->next=ptrnext->next;
ptrnext->next=ptr;
free(ptr);
free(ptrnext);
}
return start;
}
int main(void){
Node * start =takeInput();
start=insert(start);
print(start);
}
When I run the following code, it gives me an infinite looping result. However if I comment out the free pointer lines in the insert function i.e.
free(ptr)
andfree(ptrnext)
then it works fine.
这是未定义的行为。 (当您不注释
free()
函数时)释放内存后,请记住不要再使用它。
Note : the pointer might or might not point the same block after freeing, it's undefined behavior
所以不要释放指针,除非你想
destroy
或delete
节点。所以不要在
insert
函数中使用free()
,因为您没有删除任何节点。
除此之外,我没有看到任何函数可以在程序结束时释放内存。
始终确保在最后使用
delete()
函数 释放 分配的内存 。这是
delete
函数的典型实现void delete(Node* start) { Node* temporary = NULL; while(start != NULL) { temporary = start->next; //saving next node address free(start); //freeing current node start = temporary; //assigning start with next node address } printf("successfully destroyed the list!"); //function exit message }
在
main()
函数末尾或当您希望delete
整个列表时调用它