[英]How do I access a null pointer (node) of a linked list?
This method is supposed to append a Node at the end of a linked list.这个方法应该是把 append 一个 Node 放在一个链表的末尾。 The method loops until it reaches the end, which is the null pointer.该方法循环直到到达末尾,即 null 指针。 But when I try to change the null pointer to a value, it crashes.但是当我尝试将 null 指针更改为一个值时,它会崩溃。 How should I fix this?我应该如何解决这个问题? (The Node pointer has a integer data and another Node variable which the current Node points to). (Node 指针有一个 integer 数据和当前 Node 指向的另一个 Node 变量)。
void appendItem(LinkedList* list, int value)
{
Node* temp = (Node*)malloc(sizeof(Node));
temp = list->head;
while(temp != NULL)
{
temp = temp->next;
}
temp->data = value;
temp->next = NULL;
}
Dereferencing NULL
is prohibited.禁止取消引用NULL
。
Instead of that, you should manage a pointer to what should be changed.取而代之的是,您应该管理指向应该更改的内容的指针。
Also note that allocating some buffer via malloc()
and overwriting the result with another value just after that causes a memory leak.另请注意,通过malloc()
分配一些缓冲区并在其后用另一个值覆盖结果会导致 memory 泄漏。
One more point is that casting results of malloc()
family is considered as a bad practice .还有一点是malloc()
系列的强制转换结果被认为是一种不好的做法。
Fixed code:固定代码:
void appendItem(LinkedList* list, int value)
{
Node** temp = &list->head;
while(*temp != NULL)
{
temp = &(*temp)->next;
}
*temp = malloc(sizeof(Node));
if (*temp != NULL)
{
(*temp)->data = value;
(*temp)->next = NULL;
}
}
These lines这些行
Node* temp = (Node*)malloc(sizeof(Node));
temp = list->head;
produces a memory leak.产生 memory 泄漏。 At first a memory was allocated and its address was stored in the pointer temp
and then the value of the pointer temp
was overwritten by the value of the expression list->head
.首先分配了 memory 并将其地址存储在指针temp
中,然后指针temp
的值被表达式list->head
的值覆盖。 As a result the address of the allocated memory was lost.结果,分配的 memory 的地址丢失了。
After this loop在这个循环之后
while(temp != NULL)
{
temp = temp->next;
}
the pointer temp
is equal to NULL
.指针temp
等于NULL
。 So a null pointer is used to access memory in these statements因此在这些语句中使用 null 指针来访问 memory
temp->data = value;
temp->next = NULL;
that invokes undefined behavior.调用未定义的行为。
The function can be defined for example the following way.例如,function 可以通过以下方式定义。
int appendItem( LinkedList *list, int value )
{
Node *new_node = malloc( sizeof( Node ) );
int success = new_node != NULL;
if ( success )
{
new_node->data = value;
new_node->next = NULL;
if ( list->head == NULL )
{
list->head = new_node;
}
else
{
Node *current = list->head;
while ( current->next != NULL ) current = current->next;
current->next = new_node;
}
}
return success;
}
Pay attention to that the memory allocation can fail.请注意,memory 分配可能会失败。 You need to process such a case in your function.您需要在 function 中处理这种情况。 And the caller of the function should be informed about such a situation. function 的调用者应该被告知这种情况。
Also as you allow to append new nodes to a singly-linked list then the list should be defined as a two-sided singly-linked list.此外,当您允许 append 将新节点添加到单链表时,该列表应定义为双边单链表。 That is the list should keep two pointers: one pointer to the head node and other pointer to the tail node.也就是说,列表应该保留两个指针:一个指向头节点的指针,另一个指向尾节点的指针。 Otherwise appending a node to the list will be inefficient.否则将节点附加到列表将是低效的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.