如何阻止STL容器调用超载的呼叫 [英] How to stop STL containers from calling call overloaded new

查看:59
本文介绍了如何阻止STL容器调用超载的呼叫的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个内存池,其中正在使用STL映射来存储有关已分配内存的信息.我在全局范围内重载了new/delete.问题是地图正在调用重载的new,这造成了一些问题.



  int  * iptr =   int  [ 10 ] ---->应该调用重载的 []
删除 [] iptr ------------->应该调用重载的删除 []
map.insert( 10 )-------------> ;;应该调用默认  [](其中的>   .h)
map.erase()--------------->应该调用默认 删除 [] // (位于new.h中) 



解决方案

您有几种选择...

0.不要全局重载new/delete(您可以始终定义自己的分配器).
1.分别调用需要不同分配器的文件(如John建议的那样).
2.使用新的放置而不是一起覆盖所有放置(应该是存储池所需的全部).


这里的问题是,一旦您覆盖全局新闻并删除了您, "将您的新版本设为默认版本.它们是万不得已的选择,如果它们失败,则运行时库将不会插入另一个版本:您定义的new/delete甚至会阻止运行时库随附的版本被链接.

无论如何,这里有三个建议:

-创建一个自定义分配器,并让地图使用它.您可以使用mallocfree(ugh,spit,spit,ugh)或操作系统功能(double ugh,barf,barf,heave)来实现它.分配器的界面有点笨拙,但是可以做到.

-解决问题-不要覆盖全局newdelete.而是在逐个类的基础上重写它们,这将为您提供更多控制权,也可能是效率,因为您可以为不同大小的类实现堆或池.

-使用不会自行调整大小的静态数据结构.排序大小固定的对象数组意味着您可以像std::map一样使用二进制搜索来快速访问元素.您必须分析您的应用程序才能尝试确定适合您的正确大小.

我的建议是不要覆盖任何全局newdelete.您最终会摆弄指针,并且人本不该知道的事情".覆盖全局的任何内容都有点像在程序中具有全局状态-难以进行单元测试.如果您需要自定义内容,请尝试使用特定于类的new/delete或用于集合的自定义分配器. 然后在要使用自己的新文件的文件中使用宏...

#define new my_new

我想这样做.


I am developing a Memory Pool in which I am using a STL map to store information about the allocated memory. I have globally overloaded new/delete.The problem is that map is calling the overloaded new which is creating some problem.



int *iptr = new int[10] ----> should call overloaded new[] 
delete [] iptr -------------> should call overloaded delete[] 
map.insert(10) ------------->; should call default new[] ( which are in new.h) 
map.erase()  ---------------> should call default delete[] //(which are in new.h)



Is it possible to achieve?

解决方案

You have a few options...

0. Don''t overload new/delete globally (you can always define your own allocator).
1. Separate calls into files that require different allocators (like what John suggested).
2. Use a placement new instead of overriding it all together (should be all you need for your memory pool).


The problem here is as soon as you override the global news and deletes you''re making your new versions the default. They''re the court of last resort, if they fail the runtime library won''t step in with another version: The new/delete you define will prevent the versions that come with your runtime library from even being linked.

Anyway, Here''re three suggestions:

- Create a custom allocator and let the map use it. You could use malloc and free (ugh, spit, spit, ugh) or operating system features (double ugh, barf, barf, heave) to implement it. Allocators are a bit of a fiddly interface but it can be done.

- Turn the problem on it''s head - don''t override global new and delete. Instead override them on a class by class basis, which will give you a lot more control and perhaps efficiency as you can implement heaps or pools for different sized classes.

- Use a static data structure that doesn''t resize itself. A sorted fix sized array of objects means you can access elements as fast using a binary search as a std::map. You''ll have to profile your app to try and get a handle on what''s the correct size for you.

My advice would be don''t override any of the global news and deletes. You end up fiddling with pointers and "things man was not meant to know." Overriding anything global is a bit like having global state in a program - a pain in the bum to unit test. If you need to customise stuff try using class specific new/delete or a custom allocator for for collections.


You could give your new new a new name, such as my_new.
Then in the files where you want to use your own new use a macro...

#define new my_new

I think that would do it.


这篇关于如何阻止STL容器调用超载的呼叫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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