【问题标题】:Fixing load of null pointer of type 'Node *' error修复“Node *”类型的空指针错误的加载
【发布时间】:2021-05-11 21:22:45
【问题描述】:

我正在用 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 行对应 llist.c 文件中的 toAdd->next = *list

我正在努力弄清楚为什么会发生此错误,因为我正在使用适当的参数将我的推送函数调用到最初为空 (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,可能没有赋值。

标签: c linked-list pass-by-reference singly-linked-list function-definition


【解决方案1】:

当您将指针列表初始化为空指针时

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));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-03
    • 1970-01-01
    相关资源
    最近更新 更多