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 crash when deleting a is happening because there are two pointers pointing to the same A object allocated on the heap:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.