c++ - 字符串 vector 仍然可以到达

标签 c++ vector valgrind

我有这个字符串 vector 的示例代码:

vector<string> strings;
strings.push_back(argv[0]);
cout << strings[0] << endl;
strings.clear();

exit(0);

但是 valgrind 说:

==26012== HEAP SUMMARY:
==26012==     in use at exit: 8 bytes in 1 blocks
==26012==   total heap usage: 2 allocs, 1 frees, 41 bytes allocated
==26012== 
==26012== 8 bytes in 1 blocks are still reachable in loss record 1 of 1
==26012==    at 0x4C2B1C7: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==26012==    by 0x401DCF: __gnu_cxx::new_allocator<std::string>::allocate(unsigned long, void const*) (new_allocator.h:92)
==26012==    by 0x401BD8: std::_Vector_base<std::string, std::allocator<std::string> >::_M_allocate(unsigned long) (in /home/nich/IPK/2/client)
==26012==    by 0x401706: std::vector<std::string, std::allocator<std::string> >::_M_insert_aux(__gnu_cxx::__normal_iterator<std::string*, std::vector<std::string, std::allocator<std::string> > >, std::string const&) (vector.tcc:327)
==26012==    by 0x401421: std::vector<std::string, std::allocator<std::string> >::push_back(std::string const&) (stl_vector.h:834)
==26012==    by 0x401102: main (client.cc:130)
==26012== 
==26012== LEAK SUMMARY:
==26012==    definitely lost: 0 bytes in 0 blocks
==26012==    indirectly lost: 0 bytes in 0 blocks
==26012==      possibly lost: 0 bytes in 0 blocks
==26012==    still reachable: 8 bytes in 1 blocks
==26012==         suppressed: 0 bytes in 0 blocks

我做错了什么? String::clear 方法应该在字符串上调用析构函数不应该吗? 无论如何,我尝试了使用指针的替代方法。

vector<string*> strings;
strings.push_back(new string(argv[0]));
cout << *(strings[0]) << endl;
delete strings[0];
strings.clear();

exit(0);

瓦尔格林德:

==10177== HEAP SUMMARY:
==10177==     in use at exit: 8 bytes in 1 blocks
==10177==   total heap usage: 3 allocs, 2 frees, 49 bytes allocated
==10177== 
==10177== 8 bytes in 1 blocks are still reachable in loss record 1 of 1
==10177==    at 0x4C2B1C7: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==10177==    by 0x401CF9: __gnu_cxx::new_allocator<std::string*>::allocate(unsigned long, void const*) (new_allocator.h:92)
==10177==    by 0x401B5C: std::_Vector_base<std::string*, std::allocator<std::string*> >::_M_allocate(unsigned long) (in /home/nich/IPK/2/client)
==10177==    by 0x4016EA: std::vector<std::string*, std::allocator<std::string*> >::_M_insert_aux(__gnu_cxx::__normal_iterator<std::string**, std::vector<std::string*, std::allocator<std::string*> > >, std::string* const&) (vector.tcc:327)
==10177==    by 0x4013F1: std::vector<std::string*, std::allocator<std::string*> >::push_back(std::string* const&) (stl_vector.h:834)
==10177==    by 0x4010C4: main (client.cc:130)
==10177== 
==10177== LEAK SUMMARY:
==10177==    definitely lost: 0 bytes in 0 blocks
==10177==    indirectly lost: 0 bytes in 0 blocks
==10177==      possibly lost: 0 bytes in 0 blocks
==10177==    still reachable: 8 bytes in 1 blocks
==10177==         suppressed: 0 bytes in 0 blocks

还是一样的问题。你能告诉我应该如何将参数从 *argv[] 解析为 vector 字符串而没有仍然可访问的字节吗?

最佳答案

调用strings.clear()只是清空了vector,但是vector自身还在占用内存。通过像这样静态地创建它:

vector<string> strings;

它仍然会在内存中,直到函数超出范围(并且由于您在函数中调用了 exit,所以当程序结束时它仍在范围内)。

关于c++ - 字符串 vector 仍然可以到达,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15307446/

相关文章:

math - 非规范化向量

c++ - 更改 C++ vector 的保留内存

C++ 自定义字符串修剪实现 valgrind 警告

C 程序在 Cygwin 中运行但在 Linux (Malloc) 中不运行

c++ - 如何将尾随返回类型与模板化类成员一起使用

c++ - find_if 错误 : invalid initialisation of reference of type 'const node&' from expression of type 'node*'

c++ - 一般处理文件流

c++ - C++ 中的动态和静态库

c++ - 调用 wcstombs 时 Valgrind 报告未初始化的值

c++ - 排序多维数组/vector