简体   繁体   English

c++ memory管理

[英]c++ memory management

Is this a good way to delete a map of longs and objects made with new这是删除 map 的好方法吗?

// iterate over the map
for (std::map<unsigned long, Object*>::iterator it = objects.begin(), it_end = objects.end(); it != it_end; ++it)
{
    Object* temp = it->second;
    if(temp)
        delete temp;
}

// clear the map
objects.clear();

Yes.是的。 Use boost::ptr_map使用 boost::ptr_map

boost::ptr_map<std::string, BigObject>   data;

data.insert("Plop", new BigObject);

When data goes out of scope it deletes all its value members.当数据离开 scope 时,它会删除其所有value成员。
Also for algorithms all members are returned as reference to the object (not pointer) thus it is much easier to use with the standard algorithms than a std::map<std::string, BigObject*> where you would need to de-reference the memebrs before use.同样对于算法,所有成员都作为对 object (不是指针)的引用返回,因此与标准算法一起使用比 std::map<std::string, BigObject*> 您需要取消引用要容易得多使用前的成员。

One would have to question why you have a map of pointer to int/long in the first place?人们不得不质疑为什么你首先有一个指向 int/long 的 map 指针? Would it not be easier to just to store the value in the map?将值存储在 map 中不是更容易吗?

Yes, although the better solution would be to use smart pointers instead of Object* , but that is a different subject.是的,虽然更好的解决方案是使用智能指针而不是Object* ,但这是一个不同的主题。

You could shorten the content of the for to您可以将 for 的内容缩短为

{
   delete it->second;
}

delete null is defined and is a noop. delete null已定义并且是一个 noop。

It is a perfectly fine way to delete.这是一个非常好的删除方法。

Though you can make your life much easier by using a smart pointer.尽管您可以使用智能指针让您的生活更轻松。

It's a good start.这是一个好的开始。

Once you've deleted the object, you should either remove the pointer from the map or set it to NULL to avoid dangling pointers.删除 object 后,应从 map 中删除指针或将其设置为 NULL 以避免悬空指针。 Edit: or of course you could just clear out the map when you're done, as your example shows.编辑:或者你当然可以在完成后清除 map,如示例所示。

When you store pointers in any of the standard containers, there's always a possibility that an exception or some code error will result in a memory leak.当您将指针存储在任何标准容器中时,异常或某些代码错误总是有可能导致 memory 泄漏。 I'd suggest boost pointer containers as an alternative.我建议将提升指针容器作为替代方案。

Yes.是的。 Regardless of what might be considered a design or architecture issue (the use of smarter pointers, for example), it is not unreasonable to use that method to clear the map.无论什么可能被视为设计或架构问题(例如,使用更智能的指针),使用该方法清除 map 并非不合理。

One proviso: the objects map should not by changed by the destruction of the Object* pointers.一个附带条件: objects map 不应因 Object* 指针的破坏而改变。 If it is, it might be better to use something like this:如果是这样,最好使用这样的东西:

// iterate over the map
typedef std::map<unsigned long, Object*>::iterator map_iter;
for (map_iter it = objects.begin();
    it != objects.end();
    /* blank */ )
{
    iter_map to_delete = it;
    ++it;
    Object* temp = to_delete->second;
    if(temp)
        delete temp;
    objects.delete(to_delete);
}

I would recommend the use of a smart pointer, for example std::unique_ptr (this is a C++0x feature, not all compilers support it yet).我建议使用智能指针,例如std::unique_ptr (这是 C++0x 功能,并非所有编译器都支持它)。 For example:例如:

std::map<unsigned long, std::unique_ptr<Object>> map;
// Do something with the map
map.clear(); // The objects are automatically deleted.

You can also use std::shared_ptr (or boost::shared_ptr if your compiler doesn't support C++0x smart pointers), which has the advantage that it will work if your map can contain the same pointer more than once, and your objects won't be destroyed if someone else still has a pointer to them.您还可以使用std::shared_ptr (如果您的编译器不支持 C++0x 智能指针,则使用boost::shared_ptr ),其优点是如果您的 map 可以多次包含相同的指针,它将起作用,并且如果其他人仍然有指向它们的指针,您的对象将不会被销毁。

boost::ptr_map is also an options, though I believe that, like the manual approach, will not work right if the map contains the same pointer more than once. boost::ptr_map也是一个选项,但我相信,如果 map 多次包含相同的指针,则与手动方法一样,它将无法正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM