链表析构函数

发布于 2024-11-18 05:33:56 字数 938 浏览 2 评论 0原文

我正在利用自己的时间学习 C++,并编写一个链表来尝试掌握它的窍门。我担心我删除该对象的方式。这是一个单链表。这是析构函数:

template <typename T>
LinkedList<T>::~LinkedList() 
{
  Node<T> * current = this->first;
  do {
    Node * temp = current->next;
    delete current; // THIS JUST MIGHT BE A TERRIBLE IDEA!!!
    Node * current = temp; // new current-- might work with the current
                           // delete a line above
  } while (current->next != 0); // need to leave this->last so that I don't
                                // delete it twice in the next line.
                                // Just realized I'm deleting this->first, then 
                                // in the next line [implicitly] deleting it again!
                                // 
  delete this;
}

我创建一个指向列表中第一个节点的指针,创建一个指向下一个节点的临时指针,删除第一个指针,创建一个具有相同名称的新指针,然后循环返回。完成后,它删除“this”指针。

我确信您可以明白为什么我对创建与已删除指针同名的新指针的方式感到担心。

I'm studying C++ on my own time, and writing a linked list to try and get the hang of it. I'm worried about the way I've come up to delete the object. It's a singly linked list. Here's the destructor:

template <typename T>
LinkedList<T>::~LinkedList() 
{
  Node<T> * current = this->first;
  do {
    Node * temp = current->next;
    delete current; // THIS JUST MIGHT BE A TERRIBLE IDEA!!!
    Node * current = temp; // new current-- might work with the current
                           // delete a line above
  } while (current->next != 0); // need to leave this->last so that I don't
                                // delete it twice in the next line.
                                // Just realized I'm deleting this->first, then 
                                // in the next line [implicitly] deleting it again!
                                // 
  delete this;
}

I create a pointer to the first node in the list, create a temporary pointer to the next node, delete the first pointer, create a new pointer with the same name, which then loops back. After it's done, it deletes the 'this' pointer.

I'm sure you can see why I'm worried with the way I create a new pointer with the same name as a deleted pointer.

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

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

发布评论

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

评论(4

携余温的黄昏 2024-11-25 05:33:56
  1. 不要在析构函数中删除它
  2. 如果 Node 是模板,那么您需要在所有这些定义中编写 Node
  3. 不要重新定义current,只需为其分配一个新值。

除此之外,我在这段代码中没有看到任何其他问题。

  1. Don't delete this in the destructor.
  2. If Node is a template, then you need to write Node<T> in all of those definitions.
  3. Don't redefine current, just assign it a new value.

Other than that, I don't see any other problems in this snippet.

农村范ル 2024-11-25 05:33:56

为什么不编译代码,尝试一下,看看会发生什么?最糟糕的事情是你的程序崩溃了,你必须找出原因。

您的代码基本上应该可以工作,只是您需要在 while 循环条件中测试 current 而不是 current->next 并且编写 current->next 是多余的(并且可能是错误的) code>delete this 在析构函数中,还有一些 Cat Plus Plus 在他的回答中指出的错误。

如果您正在尝试学习 C++,那么您应该学习更多内容,直到您了解此处所犯的错误,并确信固定代码能够正常工作。

这是我的函数的固定版本:

template <typename T> LinkedList<T>::~LinkedList() 
{
    Node<T> * current = this->first;
    while(current != 0) {
        Node<T> * temp = current->next;
        delete current;
        current = temp;
    }
    delete this;
}

Why not compile the code, try it, and see what happens? The worst thing that would happen would be that your program crashes and you have to figure out why.

Your code should basically work except that you need to test current in the while loop condition instead of current->next and it is redundant (and probably wrong) to write delete this in a destructor, and there are some more errors that Cat Plus Plus pointed out in his answer.

If you're trying to learn C++, then you should learn more of it to the point where you understand the mistakes you made here and are confident that the fixed code will work.

Here is my fixed version of the function:

template <typename T> LinkedList<T>::~LinkedList() 
{
    Node<T> * current = this->first;
    while(current != 0) {
        Node<T> * temp = current->next;
        delete current;
        current = temp;
    }
    delete this;
}
瀟灑尐姊 2024-11-25 05:33:56

我没有看到这个问题,但我看到很多错误:

template <typename T>
LinkedList<T>::~LinkedList() 
{
    Node<T>* current = this->first; // you never check if this->first is 0
    do {
        Node * temp = current->next;
        delete current; // THIS is not a problem
        Node * current = temp; /* this line has no effect -
                     you define new variable and it disappears when reaches
                     the end of its scope next line */
    } while (current->next != 0); /* 'current' is always 'this->first' but
              even if you would assign it 'temp' like you intended few lines
              above you never check if 'current' is 0 so you will
              dereference 0 when you reach the end of the list */

    delete this; /* this line is total nonsense
            if your LinkedList is created with 'new LinkedList' then
            you have infinite recursion (you call destructor from destructor)
            otherwise you 'delete' pointer that you never 'new'-ed */
}

正确的代码是这样的:

template <typename T>
LinkedList<T>::~LinkedList() 
{
    Node<T>* current = this->first;
    while (current != 0)
    {
        Node<T>* temp = current->next;
        delete current;
        current = temp;
    }
}

I don't see the question but I see lots of errors:

template <typename T>
LinkedList<T>::~LinkedList() 
{
    Node<T>* current = this->first; // you never check if this->first is 0
    do {
        Node * temp = current->next;
        delete current; // THIS is not a problem
        Node * current = temp; /* this line has no effect -
                     you define new variable and it disappears when reaches
                     the end of its scope next line */
    } while (current->next != 0); /* 'current' is always 'this->first' but
              even if you would assign it 'temp' like you intended few lines
              above you never check if 'current' is 0 so you will
              dereference 0 when you reach the end of the list */

    delete this; /* this line is total nonsense
            if your LinkedList is created with 'new LinkedList' then
            you have infinite recursion (you call destructor from destructor)
            otherwise you 'delete' pointer that you never 'new'-ed */
}

The correct code is this:

template <typename T>
LinkedList<T>::~LinkedList() 
{
    Node<T>* current = this->first;
    while (current != 0)
    {
        Node<T>* temp = current->next;
        delete current;
        current = temp;
    }
}
長街聽風 2024-11-25 05:33:56
~LinkedList
{
//...
  delete this;
}

析构函数中的delete this; 就像代码自杀。您的对象已经被破坏,并且您再次使用 delete this; 进行破坏。这是一种未定义的行为。你可以删除它。其余的事情看起来都很好(假设 this->first 给出了头 Node)。

编辑:我错过了,你重新定义了当前。删除它。 (应该简单地current = temp;

~LinkedList
{
//...
  delete this;
}

delete this; in a destructor is like a code-suicide. Your object is already getting destructed and you are again destructing with delete this;. It's an undefined behavior. You can remove that. Rest of things look fine (assuming that this->first gives the head Node).

Edit: I missed that, you have redefined current. Remove that. (should be simply current = temp;)

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