除非您正在编写操作系统或嵌入式系统的一部分,否则有什么理由这样做吗?我可以想象,对于一些经常创建和销毁的特定类,重载内存管理功能或引入对象池可能会降低开销,但在全局范围内做这些事情吗?
Unless you're programming parts of an OS or an embedded system are there any reasons to do so? I can imagine that for some particular classes that are created and destroyed frequently overloading memory management functions or introducing a pool of objects might lower the overhead, but doing these things globally?
添加
我刚刚在重载的删除函数中发现了一个错误 - 内存并不总是被释放.那是在一个不太重要的内存应用程序中.此外,禁用这些重载只会降低约 0.5% 的性能.
Addition
I've just found a bug in an overloaded delete function - memory wasn't always freed. And that was in a not-so memory critical application. Also, disabling these overloads decreases performance by ~0.5% only.
出于多种原因,我们重载了我工作的全局 new 和 delete 运算符:
We overload the global new and delete operators where I work for many reasons:
新建/删除记帐的想法非常灵活和强大:例如,您可以在发生分配时记录活动线程的整个调用堆栈,并汇总有关的统计信息.如果您没有空间将堆栈信息保存在本地,无论出于何种原因,您都可以通过网络发送堆栈信息.您可以在此处收集的信息类型仅受您的想象力(当然还有性能)的限制.
The idea of new/delete accounting is really flexible and powerful: you can, for example, record the entire callstack for the active thread whenever an alloc occurs, and aggregate statistics about that. You could ship the stack info over the network if you don't have space to keep it locally for whatever reason. The types of info you can gather here are only limited by your imagination (and performance, of course).
我们使用全局重载是因为它可以方便地将许多常见的调试功能挂在那里,并根据我们从这些重载中收集的统计数据对整个应用进行全面改进.
We use global overloads because it's convenient to hang lots of common debugging functionality there, as well as make sweeping improvements across the entire app, based on the statistics we gather from those same overloads.
我们仍然为个别类型使用自定义分配器;在许多情况下,您可以通过为例如提供自定义分配器来获得加速或功能.STL 数据结构的单点使用远远超过您可以从全局重载中获得的一般加速.
We still do use custom allocators for individual types too; in many cases the speedup or capabilities you can get by providing custom allocators for e.g. a single point-of-use of an STL data structure far exceeds the general speedup you can get from the global overloads.
看看一些用于 C/C++ 的分配器和调试系统,你会很快想到这些和其他想法:
Take a look at some of the allocators and debugging systems that are out there for C/C++ and you'll rapidly come up with these and other ideas:
(一本古老但开创性的书是Writing Solid Code,其中讨论了许多您可能希望在 C 中提供自定义分配器的原因,其中大部分仍然非常相关.)
(One old but seminal book is Writing Solid Code, which discusses many of the reasons you might want to provide custom allocators in C, most of which are still very relevant.)
显然,如果您可以使用这些优秀工具中的任何一个,您会希望这样做,而不是使用自己的工具.
Obviously if you can use any of these fine tools you will want to do so rather than rolling your own.
在某些情况下,它更快、更容易、业务/法律麻烦更少、平台尚无可用内容,或者只是更具指导意义:深入研究并编写全局过载.
There are situations in which it is faster, easier, less of a business/legal hassle, nothing's available for your platform yet, or just more instructive: dig in and write a global overload.
这篇关于有什么理由让全局 new 和 delete 过载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!