[英]Link list program crash on malloc
I'm fairly new to C and coding in general so please bear with me. 我对C和编码一般还是比较陌生,所以请多多包涵。 I've been trying to implement a linked list recently and this is the code i came up with 我最近一直在尝试实现链接列表,这是我想出的代码
typedef struct something{
int data;
struct something *next;
} thing ;
int main ()
{
thing *head, *current;
head=malloc(sizeof(thing));
puts("head=malloc(sizeof(thing));");
if (head != NULL)
puts("malloc success");
head=NULL;
current=head;
puts("current=head;");
if (current == NULL)
puts("current is NULL");
puts("while");
while (current!=NULL)
{
current = current->next;
}
puts("end while");
current->next=malloc(sizeof(thing));
puts("current->next=malloc(sizeof(thing));");
//free at end of program
}
While the compiler shows 0 errors, when i run the program it only runs until the final malloc
part before crashing. 虽然编译器显示0错误,但当我运行该程序时,它只会运行到崩溃前的最后一个malloc
部分。 It doesnt run the final puts
so i will assume it's something to do with the way i'm trying to use malloc. 它不会运行最终的puts
因此我将认为这与我尝试使用malloc的方式有关。 I'll gladly appreaciate for someone to tell me what im doing wrong. 我很乐意找人告诉我我做错了什么。
The problem is that your while
loop goes to far. 问题是您的while
循环进行得太远了。 You want to stop when current
points to the last element of the list, so you can add to it. 当current
指向列表的最后一个元素时,您想停止,因此可以添加到列表中。 But you're going one step further, and stopping when current == NULL
. 但是,您将进一步前进,并在current == NULL
时停止。 It's then too late to assign to current->next
. 然后为时已晚,要分配给current->next
。
First, you need to initialize head->next
to NULL. 首先,您需要将head->next
初始化为NULL。
head = malloc(sizeof(thing));
head->next = NULL;
Get rid of the line: 摆脱这条线:
head = NULL;
as this is overwriting the result of malloc()
. 因为这将覆盖malloc()
的结果。
Then your while loop needs to test current->next
, not current
itself: 然后,您的while循环需要测试current->next
,而不是current
本身:
while (current->next != NULL) {
current = current->next;
}
And when you add the new node, you have to set its next
pointer to NULL
as well: 添加新节点时,还必须将其next
指针设置为NULL
:
current->next = malloc(sizeof(thing));
current->next->next = NULL;
These should fix your problem. 这些应该可以解决您的问题。
You allocate head
and then immediately after few checks point its pointer to NULL
您分配head
,然后在经过几次检查后立即将其指针指向NULL
// Allocation here
head=malloc(sizeof(thing));
puts("head=malloc(sizeof(thing));");
// Not a null
if (head != NULL)
puts("malloc success");
// Point to NULL again ???
head=NULL;
Then your current
points to head
viz NULL
again that makes current
NULL
然后您的current
指针再次指向head
即viz NULL
,从而使current
NULL
current=head;
puts("current=head;");
if (current == NULL)
puts("current is NULL");
and then you dereference current
and try to malloc
然后取消引用current
并尝试进行malloc
puts("while");
while (current!=NULL)
{
current = current->next;
}
puts("end while");
current->next=malloc(sizeof(thing)); //current is NULL here NULL->next is invalid
puts("current->next=malloc(sizeof(thing));");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.