内存释放过程 [英] Memory release process

查看:188
本文介绍了内存释放过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好:

i写一个双重列表,我想为this.in CNode类编写 RemoveAll 函数:

Hi all :
i writing a double list and i want to write RemoveAll function for this.in CNode class :

class CNode
{
     private :
        CNode* pNext;
        CNode* pPrev;
        void*  pObj;
     public :
        ...
        ~CNode(void)
         {
             delete pObj;
         }
}



但是编译器发出警告无法删除带有指向-void-typevoid *的表达式。 />
i使用new运算符创建pObj;

对我来说非常重要的是内存释放正确(我使用微控制器并且它只有32 KB内存)。

请解释我如何删除无效指针指向它的对象(我不知道什么是pObj类型,不能使用类型转换操作符)。


but compiler gives a warning ""can not delete expression with pointer to-void-type "void *".
i create pObj with new operator;
it is very important for me that memory release correctly(i working with microcontrollers and it have only 32 KB ram).
please explain me how i can remove a object that void pointer point to it(i do not know what pObj type and can not use type casting operator).

推荐答案

当然。应该删除多少内存?使用无类型(void)指针,编译器无法找到它。



使用C ++的一种有效方法就是这样。节点对象 obj 可以是一些抽象基类型,带有虚析构函数。实际的 obj 对象的所有运行时类型都将从这个基类型派生,覆盖析构函数,这将是一种实现此对象发布的机制,无论如何它是复杂的。



最简单的机制之一就是拥有一些固定类型的数组。可以根据内存分配器通过运算符已知的当前长度删除数组 delete []

http://www.cplusplus.com/reference/new/operator%20delete%5B%5D/ [<一个href =http://www.cplusplus.com/reference/new/operator%20delete%5B%5D/target =_ blanktitle =New Window> ^ ]。



-SA
Of course. How much of memory should be deleted? With untyped (void) pointer, a compiler could not find it out.

One of the valid approaches with C++ is this. Node object obj can be of some abstract base type, with virtual destructor. All the runtime types of the actual obj objects will be derived from this base type, overriding the destructor, which would be a mechanism to implement the release for this object, no matter how complex it is.

One of the simplest mechanisms would be to have an array of some, say, fixed type. Arrays can be deleted based on the current length known to the memory allocator via the operator delete[]:
http://www.cplusplus.com/reference/new/operator%20delete%5B%5D/[^].

—SA


这篇关于内存释放过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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