STL映射和内存管理(clear()) [英] STL map and memory management (clear() )
问题描述
你好,
我有一张这样的STL地图(地图< string,Object *>
xyz;)
当我调用xyz.clear()时会发生什么?
仅清除地图或地图和对象,以便内存
再次免费?
我是否要删除每一个对象?
感谢您的回复,
Jan
Hi there,
i''ve got an STL map with something like this ( map<string, Object*>
xyz; )
What happens when I call xyz.clear()?
Is only the map cleared or the map and the Objects, so that the memory
is free again?
Have I to delete every single Object?
Thanks for replying,
Jan
推荐答案
Jan写道:
你好,
我有一张这样的STL地图(地图< string,Object *>
xyz;)
当我调用xyz.clear()时会发生什么?
只清除地图或地图和对象,以便记忆
再次免费?
我是否要删除每一个对象?
感谢您的回复,
Jan
Hi there,
i''ve got an STL map with something like this ( map<string, Object*>
xyz; )
What happens when I call xyz.clear()?
Is only the map cleared or the map and the Objects, so that the memory
is free again?
Have I to delete every single Object?
Thanks for replying,
Jan
clear()清空地图的内容。对象*没有做任何事情
;特别是它不会删除它们。
clear() empties out the contents of the map. It doesn''t do anything
with the Object*; in particular it doesn''t delete them.
谢谢:-)
thanks :-)
Jan写道:
你好,
我有一个这样的STL地图(地图< string,对象*>
xyz;)
会发生什么当我调用xyz.clear()?
只清除地图或地图和对象,以便内存
再次免费?
我是否要删除每一个对象?
感谢您的回复,
Jan
Hi there,
i''ve got an STL map with something like this ( map<string, Object*>
xyz; )
What happens when I call xyz.clear()?
Is only the map cleared or the map and the Objects, so that the memory
is free again?
Have I to delete every single Object?
Thanks for replying,
Jan
没有STL容器会删除指针类型。
我建议你使用智能指针而不是STL
容器的原始指针。
你不能将auto_ptr用于STL容器,但你可以使用其他
常见的智能指针,如boost :: shared_ptr和克隆指针,如
copy_ptr和cow_ptr。
http ://www.boost.org/libs/smart_ptr/shared_ptr.htm
http://code.axter.com/cow_ptr.h
None of the STL containers will delete a pointer type.
I recommend you use smart pointers instead of raw pointers with STL
containers.
You can not use auto_ptr with STL containers, but you can use other
common smart pointers like boost::shared_ptr and clone pointers like
copy_ptr and cow_ptr.
http://www.boost.org/libs/smart_ptr/shared_ptr.htm
http://code.axter.com/copy_ptr.h
http://code.axter.com/cow_ptr.h
这篇关于STL映射和内存管理(clear())的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!