如何销毁对象,但不能释放内存? [英] How can I destroy an object, but not free the memory?

查看:98
本文介绍了如何销毁对象,但不能释放内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在编写一个内存池应用程序,但遇到以下问题:

当我开始时,应用程序正在分配(通过使用malloc)大量的内存.然后,一个char指针列表指向分配的内存的不同位置.当我调用分配时,将返回这些指针之一.

问题是,当我调用deallocate时,我只想显式地调用对象的析构函数,然后使内存块再次可用.换句话说,我不想释放内存.

所以我的解除分配看起来像这样:

Hello people,

I am writing a memory pool application and I have the below problem:

When I start, the application is allocating (by using malloc) a big amount of memory. Then, a list of char pointers point to different places of the allocated memory. When I call allocate, one of these pointers is returned.

The problem is, when I call deallocate, I only want to explicitly call the destructor of the object and then make the memory block available again. In other words, I do not want to free the memory.

So my deallocate looks like that:

void Pool:: Deallocate(void* pObj)
{
.. Call object destructor here ..

m_ListOfSlices.push_back((char*)pObj);
m_AllocLeft += 1;
}


如何调用pObj指向的对象的析构函数?

请记住,我真的不需要模板解决方案.

由于操作员删除,必须有一种方法实际上是在做同样的事情!我尝试调试运算符delete,但是我没有运气.

请帮忙!

修改后的:::
非常感谢您的回答,

要回答答案1,恐怕您实际上可以做到.您实际上可以执行pMem->~A();,然后调用delete pMem;

要回答问题2,我很早以前就读过parashift教程,问题是它们根本不适用于我的情况;).我正在尝试做另一件事.不过,非常感谢您的答复:)


How can I call the destructor of the object that pObj points to?

Keep in mind that I do not really want a template solution.

There must be a way, since operator delete, is actually doing the same thing! I tried to debug the operator delete, but I had no luck.

Please help!

Revised:::
Thanks very much for your answers,

To answer answer 1, I am afraid you can actually do that. You can actually do pMem->~A(); and then call delete pMem;

To question answer2, I have read the parashift tutorials long ago, the problem is that they do not apply to my situation at all ;) . I am trying to do a different thing. Thanks very much for the reply though :)

推荐答案

为什么您不简单地在对象中提供一个clear()方法来清理对象呢?

顺便说一句,对我来说,您的池设计看起来不是C ++:首先您要调用malloc(纯C语言),然后要操作void指针并将其转换为char指针...这似乎真的一团糟.
通常,当您想要一个池时,它主要用于存储相同类型的对象.因此,在这种情况下,您不需要做所有这些丑陋的事情.

您实际上有什么要求?您正在尝试精确地做什么?
Why don''t you simply provide a clear() method in your object that takes care of cleaning your object ?

By the way, your design of the pool doesn''t look very C++ to me: first you are calling malloc (which is pure C) and then you are manipulating void pointers and casting them to char pointers... It really seems to be a mess.
In general, when you want to have a pool, it is mainly used to store objects of the same type. So in that case there''s no need for all these ugly stuff you are doing.

What are your requirements in fact ? What are you trying to do precisely ?


让我们开始一些步骤:

  • 无需构造即可分配:仅使用malloc或强制转换new char数组的返回值.
Lets go for steps:

  • Allocate without contruct: just use malloc, or cast the return of a new char array.
A* pA;
pA = (A*)new char[sizeof(A)];


  • 构造原始分配的内容:使用new放置:


  • Construct what is rawly allocate: use placement new:

    new(pA)A(...);


  • 销毁而不取消分配:显式调用析构函数:


  • Destroy without deallocate: explicitly call the destructor:

    pA->~A();


  • 取消分配已被销毁的内容:在分配它时使用free或释放char数组


  • Deallocate what has already been destroyed: use free or free the char array as you allocated it

    delete[] (char*)pA; pA = 0;


  • 分配和构造:只需使用new


  • Allocate and construct: just use new

    pA = new A(...);


  • 销毁并取消分配:只需使用delete


  • Destroy and deallocate: just use delete

    delete pA; pA = 0;




  • 您可以调用它,但您不应该.在这里查看[^ ]有关详细信息.
    :)
    You may call it, but you shouldn''t. See here [^] for details.
    :)


    这篇关于如何销毁对象,但不能释放内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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