简体   繁体   English

什么时候需要删除指针,什么时候需要将它们设置为 0?

[英]When do I need to delete pointers, and when do I need to set them to 0?

I'm a CS student learning about C++.我是一名 CS 学生,正在学习 C++。

This is a general question, but the particular example I'm working with is making a linked list that will have nodes to store string pointers.这是一个普遍的问题,但我正在使用的特定示例是创建一个链表,该链表将具有存储字符串指针的节点。

class LinkNode {
public:
    friend class Stack;
    LinkNode* next;
    std::string* data;
    LinkNode(std::string* data, LinkNode* next) :data(data), next(next) { ; }
}

If I'm using pointers, then shouldn't I have a destructor that calls delete on both the next and data ?如果我使用指针,那么我不应该有一个析构函数在nextdata上调用delete吗?

~LinkNode() {
    delete this->data;
    delete this->next;
}

The code used in my educational material as well as samples across the internet all neglect to include this (even though we do use destructors in other contexts).我的教材中使用的代码以及互联网上的示例都忽略了包含这一点(即使我们确实在其他上下文中使用了析构函数)。 I'm trying to understand why this would not be required in this case.我试图理解为什么在这种情况下不需要这样做。

And, quite relatedly: if I'm using a pointer to delete something, do I have to set the pointer to null?而且,相当相关:如果我使用指针来删除某些东西,我是否必须将指针设置为 null?

I often see people write a deletion out like this this:我经常看到人们这样写删除:

delete ptr;
ptr = 0;

From what I understand the purpose of setting it to 0 is just to prevent it from being accidentally used, as after the delete it now points to an invalid memory address.据我了解,将其设置为 0 的目的只是为了防止它被意外使用,因为delete后它现在指向一个无效的 memory 地址。

So if I'm calling delete at the end of a function where the pointer will fall out of scope anyway, like in a pop function or the destructor, can I omit that line?因此,如果我在 function 的末尾调用delete ,其中指针将掉出 scope 无论如何,就像在弹出 function 或析构函数中一样,可以删除该行吗? Do people just include it out of habit?人们只是出于习惯而包含它吗?

  • delete pointers pointing to a valid object/variable to destroy them and free space. delete指向有效对象/变量的指针以销毁它们并释放空间。
  • Set them to NULL , nullptr or 0 (quite the same) so you can identify them as unused.将它们设置为NULLnullptr0 (完全相同),以便您可以将它们识别为未使用。

You see, if you just delete the object, the pointer itself doesn't change , so now it's pointing to a invalid space of memory.你看,如果你只是delete object,指针本身不会改变,所以现在它指向了memory的无效空间。 If you are never again going to use that pointer, then that's not a big deal, but otherwise it could be very problematic.如果您再也不会使用该指针,那么这没什么大不了的,否则可能会成问题。

So setting it to null allows you to perform checks on your pointer and be sure whether your pointer is being used or not.因此,将其设置为null允许您对指针执行检查并确定您的指针是否正在使用。


Also, to answer as your first question, and as pointed out in the comments, you have to call delete on the pointers that you have created with new .此外,作为您的第一个问题,正如评论中指出的那样,您必须在使用new创建的指针上调用delete

  • So, let's say that the data pointers of your example point to a string created outside of the list (so it behaves like a list of references).因此,假设您的示例的data指针指向在列表之外创建的字符串(因此它的行为类似于引用列表)。 In that case that pointer is not in charge to manage its content and should not be deleted.在这种情况下,该指针不负责管理其内容并且不应被删除。
  • But if, for example, each time you add a new node, you did data = new string("whatever");但是,例如,如果每次添加一个新节点时,都执行data = new string("whatever"); , then yes, you should delete them in the destructor. ,那么是的,你应该在析构函数中删除它们。

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

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