简体   繁体   English

在删除双向链表中的节点时的Segfaulting

[英]Segfaulting on deletion of a node in a doubly linked list

So what seems to be happening is that my program is not recognizing when a node is set to nullptr...I have no idea why this is. 所以似乎正在发生的事情是我的程序没有识别节点何时设置为nullptr ...我不知道为什么会这样。 Whenever I check for if(node != nullptr) it seems to pass and go right down into the statement. 每当我检查if(node != nullptr)它似乎都会通过并直接进入语句。 I may be wrong in saying this, but this is what I think is happening...maybe you may come to a different conclusion. 我说这个可能是错的,但这就是我认为正在发生的事情......也许你可能得出一个不同的结论。 Below is all of the code you should need, please let me know if you need me to post more in order to find the problem. 以下是您需要的所有代码,如果您需要我发布更多信息以便找到问题,请告诉我。

Thanks for any help ahead of time. 谢谢你提前帮助。

Here's my code 这是我的代码

void UseList::deleteNode(std::pair <unsigned, unsigned> data){
node *pre = nullptr, *del = nullptr;

// Check if its the head, if so then delete and update it
if(head->data == data){
    del = head;
    head = del->next;
    delete del;
    return;
}

pre = head;
del = head->next;

while(del != nullptr) { // <----- On the first execution, head is nullptr, so head->next should also be nullptr.  This line should not be allowing the program to descend into the code.
    if(del->data == data){ //   <----- Seg fault occurs here.
        pre->next = del->next;
        if(del = tail)
            tail = pre;
        delete del;
        break;
    }
    pre = del;
    del = del->next;
}
}

Here's a GDB log of where its seg faulting 这是一个GDB日志,其中包含了seg故障

NOTE: The list is EMPTY when this delete is called. 注意:调用此删除时,列表为EMPTY。 It should be recognizing that head->next is a nullptr and not trying to compare its data with the data to be deleted. 它应该认识到head-> next是一个nullptr而不是试图将其数据与要删除的数据进行比较。

50              while(del != nullptr) {
(gdb)
51                      if(del->data == data){
(gdb)
std::operator==<unsigned int, unsigned int> (__x=..., __y=...)
at c:/mingw/lib/gcc/mingw32/4.8.1/include/c++/bits/stl_pair.h:215
215         { return __x.first == __y.first && __x.second == __y.second; }
(gdb)

Program received signal SIGSEGV, Segmentation fault.
0x00406352 in std::operator==<unsigned int, unsigned int> (__x=..., __y=...)
at c:/mingw/lib/gcc/mingw32/4.8.1/include/c++/bits/stl_pair.h:215
215         { return __x.first == __y.first && __x.second == __y.second; }
(gdb)

Node Class Definition & Constructor 节点类定义和构造函数

class node {
        public:
            std::pair <unsigned, unsigned> data;
            node *next;
            node *prev;
            node(){
                next = nullptr;
                prev = nullptr;
            }
            ~node(){
                delete next;
            }
    };

List Constructor 列表构造函数

UseList::UseList(){
head = tail = nullptr;
head->next = head->prev = nullptr;
tail->next = tail->prev = nullptr;
}

I think it's caused by a typo. 我认为这是由一个错字造成的。

You wrote: 你写了:

   if(del = tail)
        tail = pre;

You probably meant: 你可能意味着:

   if(del == tail)
        tail = pre;

Problems that I see 我看到的问题

  1. This might not be your problem, but it's worth pointing out. 这可能不是你的问题,但值得指出。 You have: 你有:

     if(head->data == data){ 

    This will be a problem if the list is empty, ie head == nullptr . 如果列表为空,这将是一个问题,即head == nullptr

  2. The code in the constructor is not right. 构造函数中的代码不正确。 You have: 你有:

     UseList::UseList(){ head = tail = nullptr; head->next = head->prev = nullptr; // Since head is nullptr, head->next and head->prev should // produce run time errors. tail->next = tail->prev = nullptr; // Same thing here also. } 
  3. The while loop in deleteNode needs careful walk through to make sure that you are doing the right thing. deleteNodewhile循环需要仔细检查以确保您正在做正确的事情。

    You have: 你有:

     if(del->data == data){ pre->next = del->next; if(del = tail) tail = pre; delete del; break; } 

    Let's say you have 25 items in your list and the given data is the 5-th item in the list. 假设您的列表中有25个项目,并且给定的数据是列表中的第5个项目。 At that time, pre points to the 4-th node, del points to the 5-th node. 那时, pre指向第4个节点, del指向第5个节点。 Then you execute: 然后你执行:

     pre->next = del->next; // This is good. 

    Then you execute: 然后你执行:

      if(del = tail) 

    This statement points del to the tail of the list -- the 25-th node. 该语句将del指向列表的尾部 - 第25个节点。 The expression (del = tail) evaluates to `true. 表达式(del = tail)计算结果为“true”。 So, you execute: 所以,你执行:

      tail = pre; 

Now tail points to the 4-th node in the list. 现在tail指向列表中的第4个节点。

Then you execute: 然后你执行:

      delete del;

which deletes the 25-th node of the list. 删除列表的第25个节点。

Now, the list is a strange state. 现在,列表是一个奇怪的状态。 tail points to the 4-th node. tail指向第4个节点。 The 25-th node has been deleted but the 24-th node still points to it. 第25个节点已被删除,但第24个节点仍然指向它。

If head is nullptr , you cannot access head->next . 如果headnullptr ,则无法访问head->next

So error in this line: 所以这行错误:

del = head->next; // if head is nullptr

Maybe you could write it differently for the case where head is nullptr . 也许你可以在headnullptr的情况下以不同的方式编写它。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM