为什么我们要重载/覆盖new和delete?
Possible Duplicate:
Any reason to overload global new and delete?
Why should we overload/override new and delete in C++?
Give me an example of situation in which we should overload/override new, new[], delete or delete[].
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这听起来有点像家庭作业。尽管如此,我想到了以下两个用途:
valgrind
来跟踪内存泄漏。This sounds a bit like a homework assignment. Nevertheless, here are two uses off the top of my head:
valgrind
to track memory leaks.allocator
for this, though.提供分配/释放内存的实现。 new/delete 很一般。当您知道应用程序将如何使用内存时,您可以提供更高效的 new/delete 版本。例如,如果您使用很多小物体,那将非常简单。
另一种用途是提供内存泄漏检测。
这不是经常使用的机制 - 至少根据我的经验。
To provide your implementation of allocating/deallocating memory. new/delete are very general. When you know how your application is going to use memory you can provide more efficient version of new/delete. That would be very simple for example if you used a lot of small object.
Another usage would be to provide memory leak detection.
That's not very often used mechanism - at least in my experience.