[英]Pointers in stl::map
I have query regarding std::map. 我有关于std :: map的查询。
if I have a std::map like: 如果我有一个std :: map像:
std::map <T1, T2*> my_map;
T1 t;
T2* tt = new T2;
my_map[t]=tt;
who is responsible to clean this container, Will destructor of T2 will take care of it (T2* tt). 谁负责清洁此容器,T2的销毁者将负责处理(T2 * tt)。 Also if I want to retain this container throughout the program, where should I clean it.
另外,如果我想在整个程序中保留此容器,则应在哪里清洁它。
Thanks 谢谢
The map destroys the objects stored in the map. 地图销毁了存储在地图中的对象。 The map stores some
T1
objects, which will be destroyed, and it stores some T2
pointers, which will be destroyed. 该映射存储一些将被销毁的
T1
对象,并且存储一些将被销毁的T2
指针。
But it does not store actual T2
objects. 不过,这并不存储实际
T2
的对象。 So no T2
objects will be destroyed. 因此,不会破坏任何
T2
对象。
Raw pointers do not have ownership of the objects they point to. 原始指针不拥有它们指向的对象的所有权。 So when a pointer is destroyed, it will not delete whatever it points to.
因此,当指针被销毁时,它不会删除其指向的任何内容。
Generally speaking, when you have a pointer, there is no way to know if 一般来说,当您有一个指针时,无法知道是否
delete
on some random garbage in memory), delete
在记忆一些随机的垃圾), new
(if it has been allocated in another way, it should not be deleted with delete
), or new
的对象(如果已经以其他方式分配了,则不应使用delete
),或者 delete
. Which one?) delete
。哪个?) So even if you wanted to, there is no way to automatically delete an object when a pointer to it is destroyed. 因此,即使您要删除对象的指针,也无法自动删除该对象。
The destructor of T2
is called nowhere in this example, only the pointer is destroyed. 在此示例中,在任何地方都没有调用
T2
的析构函数,仅销毁了指针。 When the map is out of scope, it will destroy all pairs of elements <T1, T2*>
, but this will not call delete
on the second item. 当映射超出范围时,它将销毁所有对元素
<T1, T2*>
,但这不会在第二项上调用delete
。
You could however use boost::shared_ptr
or std::shared_ptr
(C++11) if you want reference-counted pointers. 但是,如果需要引用计数的指针,则可以使用
boost::shared_ptr
或std::shared_ptr
(C ++ 11)。
#include <boost/shared_ptr.hpp>
std::map <T1, boost::shared_ptr<T2> > my_map;
T1 t;
T2 *tt = new T2;
my_map[t] = tt; // tt is passed to a shared_ptr, ref count = 1
// when out of scope, the destructor of `boost::shared_ptr` will call `delete`.
If you needn't make copies of the objects stored in the map, you can use C++11's std::unique_ptr
. 如果不需要复制存储在地图中的对象,则可以使用C ++ 11的
std::unique_ptr
。
What Benoit said; 贝努瓦说了什么; but as an alternative consider a
boost::ptr_map
; 但可以选择使用
boost::ptr_map
; basically a map which takes ownership of contained pointer values. 基本上是一个映射,它包含所包含的指针值的所有权。
It's up to you to decide who is responsible to clean data, depending on what is your intent (for instance, your map can be a temporary storage to help perform some algorithm and you want to avoid copies). 由您决定由谁负责清理数据,具体取决于您的意图(例如,您的地图可以是临时存储,以帮助执行某些算法,并且您希望避免复制)。 However, using
boost::shared_ptr
(or std::tr1::shared_ptr
) is often the good way to go. 但是,使用
boost::shared_ptr
(或std::tr1::shared_ptr
)通常是个好方法。
This object is a local variable created in the stack(in other words not in the heap). 该对象是在堆栈中创建的局部变量(换句话说,不是在堆中)。 Once it's out of scope, it will be destroyed.
一旦超出范围,它将被销毁。
To explain a bit more, when you create an object with new
operator it's created in the heap, and you have to manually delete
it. 为了进一步说明,当您使用
new
运算符创建对象时,该对象将在堆中创建,并且您必须手动delete
其delete
。 When you instantiate it like AClass a
this will be created in the stack and when the program flow gets out of the scope it's created it will be destroyed automatically. 当像
AClass a
这样实例化它时,它将在堆栈中创建,并且当程序流超出作用域的范围时,它将被自动销毁。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.