本文介绍了检查已删除的地图条目 - &gt;撞车?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我在我的代码中使用STL地图。 我的应用程序有时会尝试从地图上删除两次。 这会导致我的崩溃目前的代码。问题可能是 方式我检查是否有必要删除一些东西(第44行和第52行 在下面的代码中)。 In下面的代码证明了问题,第65行将导致 分段错误。 1 #include< iostream> 2 #include < cstdlib> 3 #include< map> 5使用命名空间std; 6 7级MyClass 8 { 9 public: 10 MyClass(int); 11~MyClass(); 12 void show(); 13 private: 14 int nr _; 15}; 16 17 MyClass :: MyClass(int nr):nr_(nr) 18 { 19 cout<< MyClass: << nr_<< "构造" <<结束; 20} 21 22 MyClass ::〜MyClass() 23 { 24 cout<< MyClass: << nr_<< "自毁" <<结束; 25} 26 27无效MyClass :: show() 28 { 29 cout<< 显示此MyClass: << nr_<<结束; 30} 31 32 typedef map< int,MyClass *> RtdmCyclicTimerMap; 33 34 int main(无效) 35 { 36 RtdmCyclicTimerMap cyclicRtdmTimer _; 37 map< int,MyClass *> :: const_iterator cyclicRtdmIt; 38 39 //创建1& 2 40 cyclicRtdmTimer_ [1] = new MyClass(11); 41 cyclicRtdmTimer_ [2] = new MyClass(22); 42 43 //删除2 44 if(cyclicRtdmTimer_ [2]){ 45删除cyclicRtdmTimer_ [2]; 46 cyclicRtdmTimer_.erase(2); 47}否则{ 48 cout<< 删除2失败 <<结束; 49} 51 //删除2 AGAIN 52 if(cyclicRtdmTimer_ [2] ){ 53删除cyclicRtdmTimer_ [2]; 54 cyclicRtdmTimer_.erase(2); 55}否则{ 56 cout<< 删除2 [2]失败 <<结束; 57} 58 59 //显示地图 - >现在它会崩溃 60 cout<< endl<< 将显示地图条目 <<结束; 61 for (cyclicRtdmIt = cyclicRtdmTimer_.begin(); cyclicRtdmI t!= cyclicRtdmTimer_.end(); ++ cyclicRtdmIt ){ 62 int tKey = cyclicRtdmIt-> first; 63 MyClass * mcPtr = cyclicRtdmIt-> second; 64 cout< ;< map entry found:key =" << tKey<< ",valP =" << mcPtr<< ",val =" ;; 65 mcPtr-> show(); 66} 67} 68 这是因为第52行以某种方式将内部地图大小从1 增加到2个条目。 不是吗允许引用删除的条目? 必须用迭代器完成吗? I''m using an STL map in my code.My application sometimes tries to delete things twice from the map.This leads to a crash in my current code. The problem is probably theway I check whether it is necessary to delete something (line 44 and 52in below code).In the below code the problem is demonstrated, line 65 will cause asegmentation fault.1 #include <iostream>2 #include <cstdlib>3 #include <map>45 using namespace std;67 class MyClass8 {9 public:10 MyClass(int);11 ~MyClass();12 void show();13 private:14 int nr_;15 };1617 MyClass::MyClass(int nr) : nr_(nr)18 {19 cout << "MyClass: " << nr_ << " constructed" << endl;20 }2122 MyClass::~MyClass()23 {24 cout << "MyClass: " << nr_ << " destructed" << endl;25 }2627 void MyClass::show()28 {29 cout << "Show this MyClass: " << nr_ << endl;30 }3132 typedef map<int, MyClass *> RtdmCyclicTimerMap;3334 int main(void)35 {36 RtdmCyclicTimerMap cyclicRtdmTimer_;37 map<int, MyClass *>::const_iterator cyclicRtdmIt;3839 // create 1 & 240 cyclicRtdmTimer_[1] = new MyClass(11);41 cyclicRtdmTimer_[2] = new MyClass(22);4243 // delete 244 if (cyclicRtdmTimer_[2]) {45 delete cyclicRtdmTimer_[2];46 cyclicRtdmTimer_.erase(2);47 } else {48 cout << "delete 2 failed" << endl;49 }5051 // delete 2 AGAIN52 if (cyclicRtdmTimer_[2]) {53 delete cyclicRtdmTimer_[2];54 cyclicRtdmTimer_.erase(2);55 } else {56 cout << "delete 2[2] failed" << endl;57 }5859 // show map -> NOW IT WILL CRASH60 cout << endl << "going to show map entries" << endl;61 for(cyclicRtdmIt=cyclicRtdmTimer_.begin();cyclicRtdmI t!=cyclicRtdmTimer_.end();++cyclicRtdmIt) {62 int tKey = cyclicRtdmIt->first;63 MyClass *mcPtr = cyclicRtdmIt->second;64 cout << "map entry found: key=" << tKey << ", valP=" <<mcPtr << ", val=";65 mcPtr->show();66 }67 }68This is because line 52 somehow increases the internal map size from 1to 2 entries.Isn''t it allowed to refer to deleted entries ?Must it be done with an iterator ? 推荐答案 是的。使用find()成员函数而不是operator []。这绝不会是b $ b创建元素。如果找到元素,你会得到一个迭代器, 否则你得到结束()。 Yes. Use the find() member function instead of operator[]. This will nevercreate elements. If the element is found, you get an iterator to it,otherwise you get end(). 问题出在这里。这隐式地向地图添加了一个元素,并且用NULL初始化指针。当您尝试取消引用该指针 之后,您会遇到段错误。 您可以使用以下检查: if(cyclicRtdmTimer_.find(2)!= cyclicRtdmTimer_.end()){ 祝福, - Andrei Tarassov 软件工程师 Altiris,Inc。 www.altiris.com The problem is here. This implicitly adds an element to the map andinitializes the pointer with NULL. When you try to dereference that pointerlater, you get a segfault. You could use the following check instead:if (cyclicRtdmTimer_.find(2) != cyclicRtdmTimer_.end()) { Best wishes,--Andrei Tarassovsoftware engineerAltiris, Inc. www.altiris.com 好​​的,别忘了。安德烈是对的。新添加的指针被初始化为一个空指针,因为如果它不是空指针你只删除它,它就会停留在你的地图中。之后,当您尝试取消引用它时,您的程序 崩溃。 Ok, forget that. Andrei is right. The newly added pointer is initialized toa null pointer, and since you only remove it if it''s not a null pointer, itstays in your map. Later, when you try to dereference it, your programcrashes. 这篇关于检查已删除的地图条目 - &gt;撞车?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-26 19:18