链接链接析构函数引发分段错误

发布于 2025-01-20 18:12:43 字数 1490 浏览 2 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

百合的盛世恋 2025-01-27 18:12:44

您应该从delete运算符中排除当前节点,因为代码已经是对delete的响应。再次这样做就像盘旋。

因此,将您的代码修改为:

~Node()
{
    Node *current = this->next; // exclude current node
    Node *previous;

    while (current != NULL)
    {
        previous = current;
        current = current->next;
        previous->next = NULL; // avoid the delete below to propagate through list.
        delete previous;
    }
}

无需将上一个设置为null,因为它将用尽范围。但是,需要从其后继者中脱离上一个,因此为避免,灾难将再次遵循该链条。

该代码假设是列表的第一个节点,这是执行delete on的自然而然的事情。如果由于某种原因您将在列表中间的某个地方的节点上执行delete,目的是丢弃整个列表,那么您还需要重复上述循环的节点。当前节点。

You should exclude the current node from the delete operator, as the code is already a response to such a delete. Doing it again is like running in circles.

So modify your code to this:

~Node()
{
    Node *current = this->next; // exclude current node
    Node *previous;

    while (current != NULL)
    {
        previous = current;
        current = current->next;
        previous->next = NULL; // avoid the delete below to propagate through list.
        delete previous;
    }
}

There is no need to set previous to NULL, since it will run out of scope. But it is needed to detach previous from its successors, so to avoid the the destructor will follow that chain again.

This code assumes that this is the first node of the list, which would be the natural thing to execute delete on. If for some reason you would execute delete on a node somewhere in the middle of a list, with the aim to discard the whole list, then you would need to repeat the above loop also for the nodes that precede the current node.

掐死时间 2025-01-27 18:12:43

您不得删除当前节点本身,因为否则代码会调用未定义的行为。

您可以编写

~Node()
{
    delete next;
}

列表定义为指向头节点的指针

delete head;

,如果

#include <iostream>

class Node
{
private:
    int data;
    Node *next;

public:

    explicit Node( int data, Node *next = nullptr ) : data( data ), next( next )
    {
    }

    ~Node()
    {
        std::cout << "Deleting " << data << "...\n";
        delete next;
    }
};

void push_front( Node *&head, int data )
{
    head = new Node( data, head );
}

int main()
{
    Node *head = nullptr;
    const int N = 10;

    for (int i = 0; i < N; i++)
    {
        push_front( head, N - i );
    }

    delete head;
}

相反,

Deleting 1...
Deleting 2...
Deleting 3...
Deleting 4...
Deleting 5...
Deleting 6...
Deleting 7...
Deleting 8...
Deleting 9...
Deleting 10...

You may not delete the current node itself because otherwise the code invokes undefined behavior.

Instead you could write

~Node()
{
    delete next;
}

And if the list is defined as a pointer to the head node then you need to write

delete head;

Here is a demonstration program

#include <iostream>

class Node
{
private:
    int data;
    Node *next;

public:

    explicit Node( int data, Node *next = nullptr ) : data( data ), next( next )
    {
    }

    ~Node()
    {
        std::cout << "Deleting " << data << "...\n";
        delete next;
    }
};

void push_front( Node *&head, int data )
{
    head = new Node( data, head );
}

int main()
{
    Node *head = nullptr;
    const int N = 10;

    for (int i = 0; i < N; i++)
    {
        push_front( head, N - i );
    }

    delete head;
}

The program output is

Deleting 1...
Deleting 2...
Deleting 3...
Deleting 4...
Deleting 5...
Deleting 6...
Deleting 7...
Deleting 8...
Deleting 9...
Deleting 10...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文