简体   繁体   English

“抛出异常:读取访问冲突。 this->head 是 nullptr。”

[英]“Exception thrown: read access violation. this->head was nullptr.”

I'm writing code that removes every other element from a Linked List.我正在编写从链接列表中删除所有其他元素的代码。 It works when there are enough elements in the List.当列表中有足够的元素时,它会起作用。 When there are not enough elements, I'm trying to still have the code run through a while loop for other options (add, remove, etc).当没有足够的元素时,我仍然试图让代码通过while循环运行其他选项(添加、删除等)。 However, an error pops up但是,会弹出一个错误

Exception thrown: read access violation.抛出异常:读取访问冲突。 this->head was nullptr. this->head 是 nullptr。

How would I fix this?我将如何解决这个问题? I provided the function below:我在下面提供了 function:

void removeEveryOtherNode() {
    Chunk* previous = head;
    Chunk* pointer = head->next; //error happens here

    if (head == NULL) {
        cout << "Linked List is empty...Nothing to delete" << endl;
    }

    else if (pointer == NULL) {
        cout << "Not enough elements to delete every other element." << endl;
    }

    while (previous != NULL && pointer != NULL) {
        previous->next = pointer->next;
        delete pointer;
        previous = previous->next;
        if (previous != NULL) {
            pointer = previous->next;
        }
    }
    cout << "Removed every other element. Press 4 to display" << endl;
}

You should check if head is valid before dereferencing it您应该在取消引用之前检查 head 是否有效

if (head == NULL) {
     cout << "Linked List is empty...Nothing to delete" << endl;
     return;
}
if (head->next == NULL) {
     cout << "Not enough elements to delete every other element." << endl;
     return;
}

// then dereference
Chunk* pointer = head->next; 

Also, use nullptr instead of NULL .此外,使用nullptr代替NULL

暂无
暂无

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

相关问题 引发异常:读取访问冲突。 此-&gt; m_pRenderTarget为nullptr。 C ++ - Exception thrown: read access violation. this->m_pRenderTarget was nullptr. c++ 如何解决“抛出未处理的异常:读取访问冲突。 this-&gt;cc._Ptr 是 nullptr。” 错误[暂停] - How to fix “Unhandled exception thrown: read access violation. this->cc._Ptr was nullptr.” error [on hold] 抛出异常:读取访问冲突。 this-&gt;top 是 nullptr - Exception thrown: read access violation. this->top was nullptr C ++ Battle4Zion项目引发未处理的异常:读取访问冲突。 **这**是nullptr。 发生了 - C++ Battle4Zion project Unhandled exception thrown: read access violation. **this** was nullptr. occurred 如何修复“抛出异常:读取访问冲突。 **Surface** 为 nullptr。 在 SDL2 C++ 中发生” - How to fix “Exception thrown: read access violation. **Surface** was nullptr. occurred” in SDL2 C++ 抛出异常:读取访问冲突。 这是 nullptr - Exception thrown: read access violation. this was nullptr C++ 如何修复抛出的异常:写访问冲突。 这是空指针。 20号线 - C++ How do I fix Exception thrown: write access violation. this was nullptr. on Line 20 在将 head 与 nullptr 进行比较时:,,Exception throw: 读取访问冲突。 这是 0x20。” - While comparing head to nullptr: ,,Exception thrown: read access violation. this was 0x20." 抛出异常:读取访问冲突。 **pDSSearch** 为 nullptr - Exception thrown: read access violation. **pDSSearch** was nullptr 抛出C ++异常:读取访问冲突。 这是nullptr - C++ Exception thrown: read access violation. this was nullptr
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM