【问题标题】:Why am I getting this runtime error: member access within null pointer of type 'Solution::node' (solution.cpp)为什么我会收到此运行时错误:'Solution::node' (solution.cpp) 类型的空指针内的成员访问
【发布时间】:2021-02-11 06:29:01
【问题描述】:

我正在解决一个关于 leetcode 1409. Queries on a Permutation With Key 的问题,但我遇到了这个运行时错误,我不知道为什么。我无法调试此错误。

问题陈述:给定1到m之间正整数的数组查询,你必须按照以下规则处理所有查询[i](从i=0到i=queries.length-1):

In the beginning, you have the permutation P=[1,2,3,...,m].
For the current i, find the position of queries[i] in the permutation P (indexing from 0) and then move this at the beginning of the permutation P. Notice that the position of queries[i] in P is the result for queries[i].

返回一个包含给定查询结果的数组。

我的方法:我创建了一个链表来存储从 1 到 m 的整数。 然后根据每个查询,我将它传递给函数getpos(),该函数返回该查询在列表中的位置,然后根据问题陈述中给出的方向更新它。 然后将此返回值添加到结果向量中,该结果向量应该是处理完所有查询后的最终答案。

我添加了 cmets 以便更好地理解我的代码

class Solution {
public:
    struct node {
        int data;
        node* next = NULL;
    };
    node* addnode(node* head, int data) {
        if(head == NULL) {
            head = new node;
            head->data = data;
        }
        else {
            node* temp = head;
            while(temp->next != NULL) { temp = temp->next; }
            temp->data = data;
        }
        return head;
    }
    int getpos(node** head, int data) { //To get position of given query
        int count = 0;
        node* temp = *head;
        node* prev;
        while(temp->data != data) {     //runtime error:member access within null pointer of type 'Solution::node' (solution.cpp); SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior prog_joined.cpp:32:21
            prev = temp;
            temp = temp->next;
            count++;
        }
        prev->next = temp->next; //searched node deleted
        temp->next = *head;      //add the searched node to beginning of the list
        *head = temp;            //udapate head
        return count;            //we have position stored in count;
    }
    
    vector<int> processQueries(vector<int>& queries, int m) {
        node* head = NULL;
        for(int i=0;i<m;i++) { head = addnode(head,i+1); }
        int n = queries.size();
        vector<int> result;
        for(int i=0;i<n;i++) { result.push_back(getpos(&head,queries[i])); }
        return result;
    }
};

请调试并解释错误原因。我面临许多无法调试的运行时错误。

【问题讨论】:

  • 你能添加你包含的标题和你的主要功能吗?
  • 我在 leetcode 的控制台中编写这段代码,你只能在其中编写返回答案的函数,而输入/输出由 leetcode 自己处理。据我所知,我们可以假设 leetcode 中已经存在所有必要的标头。我已经添加了问题的链接,你可以粘贴我的代码并在那里运行。
  • @AdityaHugay 这里的海报不愿意去第三方网站。更好的操作方式是使用自己的编译器,编写自己的 main 函数,准备好后将代码复制粘贴到 leetcode 中。这将允许您创建自己的测试用例(非常重要)并使用自己的调试器(也非常重要)。这两件事都意味着你可以从使用 leetcode 中学到更多。

标签: c++ list runtime-error c++17


【解决方案1】:

您的add_node 函数有问题。深吸一口气,看看代码。 add_node 应该在每次调用时使用new 分配一个节点。问问自己,您的版本分配了多少次以及在什么情况下分配了一个新节点?

我确定您可以看到您的代码仅在head 等于 NULL 时分配一个新节点,因此它必须被窃听。

顺便说一句,如果你想要一个链表,为什么不使用std::list?你会避免犯下的错误。

【讨论】:

  • 它在其他地方显示了运行时错误,这就是为什么我从没想过 addnode。谢谢你指出来。我不确定在从该列表中获取元素(节点)的位置后如何删除它。
  • 这就是 C++ 通常的工作方式。崩溃是由其他地方的错误引起的。您的第二条评论是关于从std::list 中删除元素吗?只需使用std::findstd::list::erase
  • 谢谢,下次我会记住的。
猜你喜欢
  • 2021-05-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-07
  • 2011-12-18
  • 1970-01-01
  • 2023-03-15
相关资源
最近更新 更多