The [] operator in std :: map gives me a segmentation error

I have

std::map<std::string, myClass*> myMap 

then I insert as below:

 if(!myKey.empty()) { myMap[myKey] = this; } 

It once throws a segmentation error.

Why??

-3
source share
1 answer

Perhaps your myMap is no longer available. For example, it could be a reference to a remote pointer, or, much more likely, 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(); } 
+1
source

Source: https://habr.com/ru/post/1491198/


All Articles