为什么删除类实例后仍然可以访问它们 [英] Why can still access class instances after deleting them

查看:74
本文介绍了为什么删除类实例后仍然可以访问它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在构建一个具有类似堆栈功能(LIFO)的链接列表类,以保存Node类的实例:

So I'm building a a linked list class, with stack-like functionality (LIFO), to hold instances of a Node class:

enum Sides{NorthWest=0, North=1, NorthEast=2, West=3, East=4, SouthWest=5, South=6, SouthEast=7};

class Node
{
    public:
    Node(position2df pos, int id):nextNode(NULL)
    {
        position=pos;
        ID=id;
    }
    ~Node(){}

    position2df getPosition(){return position;}
    int getID(){return ID;}

    void setPeripheralID(Sides side, int id){peripheralID[side]=id;}
    int getPeripheralID(Sides side){return peripheralID[side];}

    Node *nextNode;

    private:
    position2df position;
    int ID;
    int peripheralID[8];
};

class NodeList
{
    public:
    NodeList()
    {
        root=NULL;
        end=NULL;
    }
    ~NodeList(){}

    /// Function for adding elements to the list.
    void push(position2df pos, int id)
    {
        if(root==NULL)
        {
            root=new Node(pos, id);
            end=root;
        }
        else
        {
            Node *newend=new Node(pos, id);
            end->nextNode=newend;
            end=end->nextNode;
        }
    }

    /// Function for removing objects from the list.
    Node *pop()
    {
        slider=root;
        Node *previous;
        Node *next=slider;

        for(previous=NULL; next!=NULL; next=slider->nextNode)
        {
            previous=slider;
            slider=next;
        }

        delete slider;
        end=previous;
        cout << "Can still access deleted object: " << (*slider).getID() << endl;

        return end;
    }

    private:
    Node *root;
    Node *end;
    Node *slider;
};

NodeList :: Node * pop()函数(其目的是删除最后一个元素并将前一个元素重置为列表的末尾),我在由(pointer指向)的 Node class 实例上调用delete名称)滑块。但是,即使删除它,我仍然可以访问实例并输出其成员。我不知道这是否重要,但是在删除实例时,该实例具有三个指向它的指针。它们是:

In the NodeList::Node *pop() function (whose purpose is to delete the last element and reset the previous one as the end of the list), I call delete on the Node class instance pointed by (pointer name) slider. However, even after deleting it I can still access the instance and output its members. I don't know if it matters, but the instance has three different pointers pointing to it at the time of its deletion. They are:


  • 节点*滑块;

  • 节点* end;

  • 上一个类实例的Node :: Node * nextNode;

我想此时的问题是:D

I suppose at this point a question is in order :D

如果删除后仍能访问实例成员,如何知道实例是否已正确删除?

If I can still access the instance members after I deleted it how do I know if it was properly deleted?

我的代码会导致内存泄漏吗?

Is my code going to cause a memory leak?

我猜最终的问题是:我做错什么了吗?

PS 这里有一些变量( position2df; vector2df; )来自Irrlicht游戏引擎。只是想指出这一点以避免任何混乱。

P.S. There are a couple of variables here (position2df; vector2df;) that come from the Irrlicht Game Engine. Just wanted to point that out to avoid any confusion.

请原谅我如果我对这篇文章含糊不清或不清楚,但是我在这方面并不熟练

Forgive me if I was vague or otherwise unclear with this post, but I'm not very skilled when it comes to asking questions online.

推荐答案


如果删除实例后仍然可以访问实例成员,该怎么办我知道它是否被正确删除了?

If I can still access the instance members after I deleted it how do I know if it was properly deleted?

只要信任运行时库:如果您调用 delete ,该东西被删除,句点。一旦告诉系统不再需要该内存,便放弃了它-它不再属于您了。系统使用可用内存来重新分配它,因此可以根据请求将其分配给程序的其他部分。您仍然可以访问该内存,甚至可以在放弃之前找到其中的内容,但这是未定义的行为。

Just trust the runtime library: if you call delete, the thing is deleted, period. Once you tell the system that you no longer need the memory, you gave it up - it is no longer yours. The system counts it with its available memory to redistribute, so it can give it to other parts of your program upon request. You can still access that memory, and may even find what was there before you gave it up, but that's undefined behavior.


是我的代码会导致内存泄漏?

Is my code going to cause a memory leak?

不,因为您释放了分配的内存。

No, because you release the memory that you allocated.


我猜最终的问题是:我做错什么了吗?

I guess the ultimate question is: am I doing something wrong?

是-访问已释放对象的内存是错误的(未定义的行为)。除此之外,它看起来还不错。

Yes - accessing the memory of deallocated object is wrong (undefined behavior). Other than that, it looks fine.

这篇关于为什么删除类实例后仍然可以访问它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆