谁能告诉我STL的map.insert()如何工作? [英] can anyone tell me how does stl's map.insert() works?

查看:109
本文介绍了谁能告诉我STL的map.insert()如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以告诉我stl的map.insert()如何工作吗?它叫new吗?我遇到了一个问题.我在代码中重载了new,我想map.insert也在使用重载的new而不是global new.无论如何我都可以实现这一目标..基本上我想要map.insert()调用全局new而不是重载new new

Can anyone tell me how does stl''s map.insert() works?.Does it call new . i am facing a problem .i have overloaded new in my code and i guess map.insert is also using the overloaded new instead of the global new.Is their anyway in which i could achieve this..basically i want map.insert() to call the global new and not the overloaded new

推荐答案

std::map::insert创建一个映射节点,该映射节点是包含密钥副本和密钥副本的私有结构.值,并根据实现要求调整一些指针,以便在指定键时以及迭代器遍历地图本身时可以找到该节点.

节点的创建/删除是通过std::allocator的实例进行的,方法是调用allocate并在返回的空间上调用placement new.
反之亦然,删除是通过显式调用节点析构函数,然后调用分配器deallocate将内存返回给它来进行的.

atd :: allocator的目的是分配/取消分配未初始化的内存.默认实现(如果未为其指定其他内容,则由std :: map使用)是使用new char[x]分配一个字节块作为char [],然后通过delete[]将tham返回给系统.

关于您的operator new,没有全局"或本地"过载".
void *运算符new(size_t)无非是一个函数,该函数的原型在<new>中声明,并且其编译体放置在默认情况下链接的C运行时库中.
您要做的只是将同一功能原型的另一种实现提供到链接器将解析的OBJ文件中,然后再进入默认库.因此,默认的库函数不再可见,并且任何编译后的代码(包括STL模板的实例,因为它们实例化为您自己的翻译单元)都将引用您的函数.

现在,关键是要确保您为这两个变量提供了一致且一致的重载
std::map::insert creates a map node that is a private structure containing a copy of the key and a copy of the value, and adjust some pointer according to the implementation requirement so that the node can be found when the key is specified and when the map itself is walked by iterators.

The creation / deletion of nodes happens through an instance of a std::allocator, by calling allocate and by calling a placement new on the returned space.
Vice versa, deletion is made by explicitly calling the node destructor, and then calling the allocator deallocate giving the memory back to it.

The purpose of atd::allocator is to allocate/deallocate uninitialized memory. The default implementation (used by std::map, if you don''t specify anything else for it) is to allocate a chunk of bytes as a char[] using new char[x], and returning tham to the system via delete[].

Respect to your operator new, there is no "global" or "local" "overload".
void* operator new(size_t) is nothing more than a function whose prototype is declared in <new> and whose compiled body is placed into the C runtime library that is linked by default.

What you do is just provideng another implementation of that same function prototype into an OBJ file the linker will resolve before going to the default library. Hence the default library function is not anymore visible, and whatever compiled code (including the instance of the STL templates, since they instantiate into you own translation units) will refer to your function.

The point, now, is to make sure that you provided consistent and congruent overload for both
void* oeprator new(size_t)




and

void* operator new[](size_t)


因为我可能是您覆盖一个,而STL实现正在调用另一个.

您还应该覆盖相应的


since i may be you override one and the STL implementation is calling the other.

You also should override the correspondent

void operator  delete(void*)




and

void operator delete[](void*)



通常,重载全局new/delete不是一个好习惯(因为这可能导致库之间的冲突).
考虑另一种语法,例如您可以通过
获得的语法



It''s not -in general- a good practice to overload the global new/delete (since it can lead to conflicts between libraries).
Consider a different syntax like the one you can obtain with

enum my_new_t { my_new };
void* operator new(size_t sz, my_new_t)
{
   //your own stuff
   return ::operator new(sz);
}


可以用作


that can be used as

A* pa = new(my_new)A;


这与new Anew(hes_new)A明显不同.
如果您需要STL类的其他分配方案,请考虑编写自己的allocator 类.


That''s clearly different that new A or new(hes_new)A.
An if you need another allocation scheme for STL classes, consider to write your own allocator class.


看看 http://vld.codeplex.com/ [ shared_ptr [
Have a look at Visual Leak Detector - Enhanced Memory Leak Detection for Visual C++[^]

The latest version is located at http://vld.codeplex.com/[^]

Why are you creating a memory leak detector?

Have you considered using shared_ptr[^] to manage your dynamically allocated objects?

Best regards
Espen Harlinn


成员8576081写道:
Member 8576081 wrote:

有人可以告诉我stl的map.insert如何()起作用?

can anyone tell me how does stl''s map.insert() works?

文档 [

The documentation[^] can. You have control on the allocator the map will use, it is the fourth (the second optional) template paramenter in the map declaration.


这篇关于谁能告诉我STL的map.insert()如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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