简体   繁体   English

为什么我在 null 指针错误(链表)中获得成员访问权限

[英]why do I get member access within null pointer error (linked list)

below are the error message and my code for linked list.以下是错误消息和我的链表代码。 could you please explain to me why do I get this error.你能解释一下为什么我会收到这个错误吗?

"Line 80: Char 21: runtime error: member access within null pointer of type 'Node' (solution.cpp) SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior prog_joined.cpp:85:21" “第 80 行:字符 21:运行时错误:null 类型‘节点’(solution.cpp)指针内的成员访问摘要:UndefinedBehaviorSanitizer:未定义行为 prog_joined.cpp:85:21”

struct Node{
    int val;
    Node* prev;
    Node* next;
    Node(): val(-1), prev(nullptr), next(nullptr){}
    Node(int x): val(x), prev(nullptr), next(nullptr){}
};

class MyLinkedList {
private:
    Node* head;
    Node* tail;
    size_t size;
public:
    MyLinkedList() {
        head = new Node();
        tail = new Node();
        head->next = tail;
        tail->prev = head;
        size = 0;
    }
    
    int get(int index) {
        if(index < 0 || index > size) return -1;
        Node* temp = head->next;
        while(index--) temp = temp->next;
        return temp->val;
    }
    
    void addAtHead(int val) {
         Node* newNode = new Node(val);
         Node* next = head->next;

         next->prev = newNode;
         newNode->next = next;
         head->next = newNode;
         newNode->prev = head;
         size++;
    }
    void addAtTail(int val) {
        Node* last_node = new Node(val);
        Node* temp = tail->prev;
        
        temp->next = last_node;
        last_node->prev = temp;
        last_node->next = tail;
        tail->prev = last_node;
        size++;
    }
    
    void addAtIndex(int index, int val) {
        if(index < 0 || index > size+1) return;
        if(index == 0) addAtHead(val);
        else if(index == size+1) addAtTail(val);
        else{
            Node* index_node = new Node(val);
            Node* temp = head;
            while(index--) temp = temp->next;

            index_node->next = temp->next;
            temp->next->prev = index_node;
            temp->next = index_node;
            index_node->prev = temp;
            size++;
        }
    }
    
    void deleteAtIndex(int index) {
        if(index > size) return;
        Node* temp = head;
        while(index--) temp = temp->next;
        temp->next = temp->next->next;
        temp->next->prev = temp;
        size--;
    }
};

thank you so much太感谢了

Nevermind turns out this little guy was the issue all along:没关系原来这个小家伙一直是问题:

 void addAtHead(int val) {
         Node* newNode = new Node(val);
         Node* next = head->next;

         next->prev = newNode;
         newNode->next = next;
         head->next = newNode;
         newNode->prev = head;
         size++;
    }

You intialized tail to head next, but in your addToHead function you never moved tail but added to head->next, so after one addition your new node will be the tail, and after two iteratorations, your new node will be past your tail, be ahead of your tail.你初始化了tail to head next,但是在你的addToHead function中你从来没有移动tail而是添加到head->next,所以在添加一次之后你的新节点将成为tail,并且在两次迭代之后,你的新节点将超过你的tail,领先于你的尾巴。 And then you call your addToTail function so when you do, tail->prev = last_node, remember what tail-prev was before: that's right head, so you would not have a head node anymore hence the chain is broken, next time you try to call addToHead, you cannot find your head you do not have a pointer to it, so it crashes.然后你调用你的 addToTail function 所以当你这样做时,tail->prev = last_node,记住 tail-prev 之前是什么:那是正确的头,所以你将不再有头节点,因此链被破坏了,下次你尝试调用 addToHead,你找不到你的头,你没有指向它的指针,所以它崩溃了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 运行时错误:null 类型“ListNode”指针内的成员访问 - 克隆链表 - runtime error: member access within null pointer of type 'ListNode' - Clone a linked list 链接列表中的运行时错误:“ListNode”类型的 null 指针中的成员访问 - Runtime error in Linked List :member access within null pointer of type 'ListNode' 为什么我在“ListNode”类型的 null 指针中获得此错误成员访问权限 - Why am I getting this error member access within null pointer of type 'ListNode' 为什么我会收到此运行时错误:null 类型“Solution::node”(solution.cpp) 指针中的成员访问 - Why am I getting this runtime error: member access within null pointer of type 'Solution::node' (solution.cpp) 运行时错误:“ListNode”类型的 null 指针内的成员访问 - runtime error: member access within null pointer of type 'ListNode' 我无法访问指向成员的指针。 为什么? - I can not get access to pointer to member. Why? 链表空指针错误 C++ - linked list null pointer error c++ &#39;struct ListNode&#39; 类型的空指针内的成员访问 - member access within null pointer of type 'struct ListNode' 螺旋矩阵挑战:“ListNode”类型的 null 指针内的成员访问 - Spiral matrix challenge: member access within null pointer of type 'ListNode' 我在CppCheck中发现错误了吗? 为什么在这里出现“空指针取消引用”错误? - Did I find a bug in CppCheck? Why do I get the “Null pointer dereference” error here?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM