静态成员回收内存和从异常恢复 [英] Static member reclaiming memory and recovering from an exception

查看:231
本文介绍了静态成员回收内存和从异常恢复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的作业问题


创建一个具有自己的运算符new的类。这个操作符应该
分配5个对象,并在第5'内存不足,并抛出一个
异常。还要添加一个静态成员函数来回收这个
内存。现在使用try block和catch子句创建一个main(),
调用内存恢复例程。将这些放在while循环中
表明从异常恢复并继续执行。

Create a class with its own operator new. This operator should allocate 5 objects, and on 5th 'run out of memory' and throw an exception. Also add a static member function that reclaims this memory. Now create a main () with try block and catch clause that calls the memory-restoration routine. Put these inside a while loop to demonstrate recovering from an exception and continuing execution.

现在我不想程序,但我困惑与这个问题。我可以处理新的操作符重载,但作为要求我应该创建一个静态成员release()。如果我必须在捕获中恢复,我如何恢复?我的意思是我应该删除什么对象。
或者我的release()的原型是错误的?

Now I don't want the program, but I am confused with this question. I can handle new operator overloading, but as asked I should create a static member "release()". And if I have to recover in catch, how do I recover? I mean what object should I delete. Or my prototype for release() is wrong?

编辑:

如果我要删除,我可以删除吗?删除任何对象实际上是不正确的。我看不到恢复。


Also if I have to delete, which of the 5 objects can I delete? And deleting any object would in fact be incorrect. I see no way of recovery.

推荐答案

听起来像你需要类似地址的静态列表。每当调用new时,都会将内存块的地址存储在该列表中。然后在你的发布方法中,你通过静态列表,释放内存加抛出异常。

Sounds like you would need something like a static list of addresses in your class. Whenever new is called you store the address of the memory block in that list. Then in your release method you go through the static list and free the memory plus throw the exception.

这篇关于静态成员回收内存和从异常恢复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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