c - 修复“Node *”类型的空指针错误的加载
问题描述
我正在用 C 实现一个链表数据结构。下面是我的链表实现文件 ( llist.c )的当前函数
#include "llist.h"
// Frees all allocated memory associated with the list pointers iteratively
void deleteList(Node **list) {
Node* ptr = *list;
Node* temp;
while(ptr != NULL) {
free(ptr->data);
temp = ptr;
ptr=ptr->next;
free(temp);
}
}
// Frees all allocated memory associated with a single node
void deleteNode(Node **toDelete) {
Node * del = *toDelete;
free(del->data);
free(del);
}
// Allocates memory for a new string and returns a pointer to the memory
Node *newNode(char *string) {
unsigned long len = strlen(string);
printf("length : %lu \n\n", len);
Node *temp = (Node*)malloc(sizeof(Node));
temp->data = (char*)malloc(len + 1*sizeof(char));
strcpy(temp->data, string);
temp->next = NULL;
return temp;
}
// Removes a node from the front of a list
Node *pop(Node **list) {
Node *newptr = (*list)->next;
deleteNode(list);
return newptr;
}
// Adds a node to the front of a list
void push(Node **list, Node *toAdd) {
toAdd->next = *list;
*list = toAdd;
}
// Return a list of pointers in order
void reverseOrder(Node **list) {
Node* prev = NULL;
Node* current = *list;
Node* next;
while (current != NULL) {
next = current->next;
current->next = prev;
prev = current;
current = next;
}
*list = prev;
}
// Prints the string stored in a single node
void printNode(Node *singleNode) {
printf("Data : %s", singleNode->data);
}
// Prints an entire linked list. Nodes are printed from first to last
void printLinkedList(Node *linkedList) {
Node *temp = linkedList;
while(temp!=NULL) {
printf("Data : %s", temp->data);
temp = temp->next;
}
}
在我的驱动程序文件中测试实现时,我收到以下错误
运行时错误:加载类型为“Node *”(又名“struct listNode *”)的空指针摘要:UndefinedBehaviorSanitizer:undefined-behavior llist.c:49:19
其中第 49 行对应toAdd->next = *list
于llist.c文件中的
我正在努力弄清楚为什么会发生此错误,因为我正在使用适当的参数将我的推送函数调用到最初为空(NULL)的链表。
驱动程序文件 ( testllist.c ) 供参考:
#include "llist.h"
int main (int argc, char *argv[]) {
printf("argc: %d", argc);
printf("\n\n");
int num_inputs = argc;
Node **list = NULL;
if (argc == 1) {
printf("No arguments passed.\n");
} else {
for (int i = 1; i < num_inputs; i++) {
printf("String is: %s\n", argv[i]);
Node *n = newNode(argv[i]);
printf("String is : %s\n\n", argv[i]);
push(list, n);
printLinkedList(*list);
}
reverseOrder(list);
pop(list);
deleteList(list);
}
return 0;
}
定义节点数据类型和函数的头文件 ( llist.h )
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// The listNode data type for storing entries in a linked list
typedef struct listNode Node;
struct listNode {
char *data;
Node *next;
};
// Frees all allocated memory associated with the list pointers iteratively
void deleteList(Node **list);
// Frees all allocated memory associated with a single node
void deleteNode(Node **toDelete);
// Allocates memory for a new string and returns a pointer to the memory
Node *newNode(char *string);
// Removes a node from the front of a list and returns a pointer to said node
Node *pop(Node **list);
// Adds a node to the front of a list
void push(Node **list, Node *toAdd);
// Return a list of pointers in order
void reverseOrder(Node **list);
// Prints the string stored in a single node
void printNode(Node *singleNode);
// Prints an entire linked list. Nodes are printed from first to last
void printLinkedList(Node *linkedList);
解决方案
当您将指针列表初始化为空指针
Node **list = NULL;
那么在函数内push
你不能取消引用这个指针
void push(Node **list, Node *toAdd) {
toAdd->next = *list;
*list = toAdd;
}
并且错误消息报告了这个问题。
你应该像这样声明指针
Node *list = NULL;
并将其传递给期望类型对象的函数,Node **
例如
push( &list, n );
如果函数声明为
int push( Node **list, const char * );
也就是说,它应该报告新节点是否被成功推送,并且新节点的分配应该对调用该函数的用户隐藏。
请注意,例如该功能deleteNode
没有多大意义。
void deleteList(Node **list) {
Node* ptr = *list;
Node* temp;
while(ptr != NULL) {
free(ptr->data);
temp = ptr;
ptr=ptr->next;
free(temp);
}
}
指向链表头节点的指针通过引用传递。但是在函数内,它的值没有改变。因此,在退出函数后,指向头节点的指针仍将具有其原始值。
该函数可以通过以下方式定义
void deleteList( Node **list )
{
while ( *list != NULL )
{
Node *ptr = *list;
*list = ( *list )->next;
free( ptr->data );
free( ptr );
}
}
该函数pop
不检查传递的指向链表头节点的指针是否等于 NULL。
Node *pop(Node **list) {
Node *newptr = (*list)->next;
deleteNode(list);
return newptr;
}
它还返回一个指向列表中成为当前头节点的下一个节点的指针。但是返回的指针没有在 main 中使用
pop(list);
请注意,该表达式1 * sizeof( char )
在此声明中用作初始化程序的表达式中没有意义
temp->data = (char*)malloc(len + 1*sizeof(char));
要么写
temp->data = (char*)malloc(len + 1);
或喜欢
temp->data = (char*)malloc( ( len + 1 )*sizeof(char));