c++ - 在充满指针的 std::vector 上使用 std::remove_if

标签 c++ pointers c++11 vector

我对 std::remove_if 函数有一点疑问。我的程序某处发生内存泄漏,我怀疑 erase 函数被破坏了。

实际上,我的代码中有这个

std::vector<Object*> v; // Just the constructor to show you
v.erase(std::remove_if(begin(v), end(v), [Foo f](Object *o){
    return o->containsFoo(f);
}), end(v));

但是经过一些研究,这个比之前的好吗?

v.erase(std::remove_if(begin(v), end(v), [Foo f](Object *o){
   if(o->containsFoo(f)) {
     delete o;
     return true; 
   }
   return false;
}), end(v));

或者我应该使用其他东西吗?

最佳答案

你真的应该使用智能指针而不是裸露的Object* - 要么

std::vector<std::unique_ptr<int>>

std::vector<std::shared_ptr<int>>

以合适的为准。如果您使用裸 C 风格指针,很容易错过关键的 delete(或 delete 两次)。

尽管如此,很容易看出一种方法会泄漏而另一种不会:

#include <algorithm>
#include <vector>

int main(int argc, char **)
{
    std::vector<int*> v{ new int(1), new int(-1) };

    if (argc < 2) {
        // First version
        v.erase(std::remove_if(begin(v), end(v),
                               [](int *o){
                                   return *o < 0;
                               }),
                end(v));
    } else {
        // Second version
        v.erase(std::remove_if(begin(v), end(v),
                               [](int *o){
                                   if (*o < 0) {
                                       delete o;
                                       return true;
                                   }
                                   return false;
                               }),
                end(v));
    }

    // normal cleanup
    for (int *p: v)
        delete p;

}

我在没有参数(调用第一个版本)的情况下运行它,然后使用参数(调用第二个版本)运行它。看看会发生什么:

g++ -std=c++11 -g -Wall -Wextra 34191606.cpp -o 34191606

valgrind --leak-check=full ./34191606
==16894== HEAP SUMMARY:
==16894==     in use at exit: 72,708 bytes in 2 blocks
==16894==   total heap usage: 4 allocs, 2 frees, 72,728 bytes allocated
==16894== 
==16894== 4 bytes in 1 blocks are definitely lost in loss record 1 of 2
==16894==    at 0x4C2B0E0: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==16894==    by 0x400881: main (34191606.cpp:6)
==16894== 
==16894== LEAK SUMMARY:
==16894==    definitely lost: 4 bytes in 1 blocks
==16894==    indirectly lost: 0 bytes in 0 blocks
==16894==      possibly lost: 0 bytes in 0 blocks
==16894==    still reachable: 72,704 bytes in 1 blocks
==16894==         suppressed: 0 bytes in 0 blocks
==16894== Reachable blocks (those to which a pointer was found) are not shown.
==16894== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)

valgrind --leak-check=full ./34191606 -
==16895== HEAP SUMMARY:
==16895==     in use at exit: 72,704 bytes in 1 blocks
==16895==   total heap usage: 4 allocs, 3 frees, 72,728 bytes allocated
==16895== 
==16895== LEAK SUMMARY:
==16895==    definitely lost: 0 bytes in 0 blocks
==16895==    indirectly lost: 0 bytes in 0 blocks
==16895==      possibly lost: 0 bytes in 0 blocks
==16895==    still reachable: 72,704 bytes in 1 blocks
==16895==         suppressed: 0 bytes in 0 blocks
==16895== Reachable blocks (those to which a pointer was found) are not shown.
==16895== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

请注意,在第一个版本中,您永远不会删除从 vector 中删除其指针的对象,并且该对象被报告为泄漏。在第二个版本中,没有内存泄漏。

关于c++ - 在充满指针的 std::vector 上使用 std::remove_if,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34191606/

相关文章:

方法回调的 C++11 模板别名

c++ - 为什么 unique_ptr::reset 没有带删除器的重载?

c++ - g++ (MinGW)、C++11 和 SSE

c++ - 没有g++编译器的mingw

c++ - 在它自己的指针上模板化一个类?

c++ - 2个动态数组初始化之间的区别

C - 从链表中弹出最后一项

c++ - 未在排序中使用的对上的重载运算符<

c++ - 在 Windows : Cannot open include file: 'Magick++.h' : No such file or directory 中编译 ZBar 示例时出错

pointers - 从 const 获取地址