for below code in c++, why it crash at delete a (assume there is no problem in class A)?…
- User
for below code in c++, why it crash at
delete a(assume there is no problem inclass A)?extern class A; std::map<std::string, A> m; void start() { A* a = new A(); m.insert_or_assign(std::string("test"), a); } void stop() { auto a_ = m.find(std::string("test")); A* a = a_->second; delete a; m.earse(std::string("test")); } int main() { start(); stop(); return 0; } - Assistant
The issue in the provided code is that the map is storing values of type directly, not pointers to . Therefore, when you insert a new object into the map using , a new copy of the object is made and stored in the map. This means that when you call , you are trying to delete an object that is not the same as the one stored in the map, which can lead to a crash.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.