c++ - STL map and list using in c++的一些问题

标签 c++ stl memory-leaks

您好,我想咨询一下: 我有 map 和 list

问题:

下面的析构函数实现是否正确:

   for (map<string,SymbolTableNode*>::iterator i = symbolTable.begin();
                         i != symbolTable.end(); ++i)
   {
       delete i;
   }
   symbolTable.clear();

还是我想念一些内存?

关于列表:

   list<MyClass2*> mylist;
   mylist.push_front(new MyClass());
   mylist.pop_front();

pop 会调用 delete 吗?或者在这种情况下我有内存泄漏?如果存在泄漏问题,我该怎么做才能避免呢?

谢谢。

最佳答案

没有 pop_front 不会调用删除。在弹出指针之前,您必须自己删除指针。:

list<MyClass2*> mylist;
mylist.push_front(new MyClass());
delete mylist.front();
mylist.pop_front();

析构函数应该是:

delete i->second;

关于c++ - STL map and list using in c++的一些问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5989288/

相关文章:

c++ - (unsigned char)str 与 str + 0xff 或 (unsigned int)value 与 value + 0xffff 哪个是更快的首选方法?

ios - Swift iOS 声音内存泄漏?

c++ - 如何使用 GDB 7.x 查看 STL 容器的内容

java - 这会在 JVM 中被垃圾收集吗?

c++ - 内存/速度问题的一般策略

c++ - 多个小仿函数类的共享头文件?

c++ - 运行时错误 (SIGABRT) - SPOJ

c++ - 为什么 std::forward 有两个重载?

c++ - CLion STL 容器无法在 Fedora 23 上的调试器中正确显示

c++ - 如何在 C++ 错误 C2338 中组织 map<set<multiset<int>>,int>