本文介绍了[]运算符在std :: map给我分段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个
std::map<std::string, myClass*> myMap
然后我插入如下:
if(!myKey.empty())
{
myMap[myKey] = this;
}
这有时会导致分段错误。
This sometime is throwing a segmentation fault.
为什么
推荐答案
也许您的myMap不再可访问。例如,它可能是对已删除的指针的引用,或者更可能是已删除类的成员变量:
Maybe your myMap is no longer accessible. For instance, it might be a reference to a deleted pointer, or, much more probable, a member variable of an already deleted class:
class MyClass {
public:
selfInsert(std::string myKey) {
if(!myKey.empty()) {
myMap[myKey] = this;
}
}
private:
std::map<std::string, myClass*> myMap;
}
int main()
{
MyClass *a = new MyClass();
delete a;
a->selfInsert();
}
这篇关于[]运算符在std :: map给我分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!