简体   繁体   English

C ++指针结构不会被删除

[英]C++ pointer struct does not get deleted

I have build a custom data-struct, that is something like a doubly linked RBTree, and while the logic seems fine, I don't see any memory getting released when I delete a node. 我已经建立了一个自定义的数据结构,就像一个双向链接的RBTree一样,虽然逻辑看起来不错,但是删除节点时看不到任何内存被释放。

This is my node declaration: 这是我的节点声明:

typedef struct rbtreere_node
{

public:
    NMessage *n_message;

    // Node Specific //
    enum color color_ID;    //<<--- Color for ID side of RBTreeRe
    enum color color_Price; //<<--- Color for Price side of RBTreeRe
    int id;                 //<<--- Unique Identifier.
    rbtreere_node *left_ID, *right_ID, *parent_ID, *left_Price, *right_Price, *parent_Price;

}*node;

When I call my remove function, I end with a delete n before my return. 当我调用remove函数时,我在返回之前以delete n结尾。 I checked and if I try to access any of the node n data before returning, it is still there... 我检查了一下,如果我尝试在返回之前访问任何node n数据,它仍然存在...

ie I'm calling this: 即我叫这个:

delete n;
std::cout << n->color_ID<<"\n\n";

and it does return a the node color 它确实返回节点颜色

Should I build a custom destructor for it? 是否应该为其构建自定义析构函数?

I also had the node message as a var, instead of a pointer, but that did not seem to change anything... 我也将node message作为变量而不是指针,但是似乎并没有改变任何东西。

Edit: My issue is that I do not see any drop in memory usage after deleting any or even all nodes. 编辑:我的问题是删除任何甚至所有节点后,我看不到内存使用量的任何下降。 Should I handle it differently? 我应该以不同的方式处理吗?

I checked and if I try to access any of the node n data before returning, it is still there... 我检查了一下,如果我尝试在返回之前访问任何节点n数据,它仍然存在...

delete n;
std::cout << n->color_ID<<"\n\n";

Dereferencing n after it has been deleted has undefined behaviour. 删除n后再取消引用具有未定义的行为。

Should I build a custom destructor for it? 是否应该为其构建自定义析构函数?

No. Unless n_message owns the memory it points, in which case the implicit destructor would leak that memory. n_message 。除非n_message拥有它指向的内存,否则隐式析构函数将泄漏该内存。

My issue is that I do not see any drop in memory usage after deleting any or even all nodes. 我的问题是,删除任何甚至所有节点后,我看不到内存使用量的任何下降。

Most implementations of malloc (which is what is typically used for dynamic memory allocation by the implementation of new ) never return freed memory to the operating system. malloc大多数实现(这通常是new的实现通常用于动态内存分配的实现)永远不会将释放的内存返回给操作系统。 Instead it reuses it for subsequent allocations. 而是将其重新用于后续分配。 This is a non-issue. 这不是问题。

Should I handle it differently? 我应该以不同的方式处理吗?

Besides getting rid of the "check" that has UB, no. 除了摆脱带有UB的“支票”之外,没有。 Everything appears to be as expected. 一切似乎都符合预期。

delete only deallocates memory and calls the object's destructor. delete只释放内存并调用对象的析构函数。 It doesn't wipe the memory or do anything like that. 它不会擦除内存或执行类似操作。 And unless you have some need to (security, for instance) it's just a waste of time and code. 并且除非您有一定的需要(例如安全性),否则这只是浪费时间和代码。 After delete you can (and obviously are) still point to the memory and dereference it. delete您可以(并且显然是)仍然指向内存并取消引用。 It's very dangerous to do as it is undefined behavior. 这样做是非常危险的,因为它是未定义的行为。

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

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