overloading operator delete, or how to kill a cat?

前端 未结 5 777
时光说笑
时光说笑 2021-01-19 15:15

I am experimenting with overloading operator delete, so that I can return a plain pointer to those who don\'t wish to work with smart pointers, and yet be able to control wh

5条回答
  •  生来不讨喜
    2021-01-19 15:50

    You've misused just every C++ concept possible and this causes errors. When delete p; is called for the first time C++ calls the Cat::~Cat() destructor which implicitly destroys the std::string inside the entity. When delete p; is called the second time Cat::~Cat() is rerun and it reruns the destructor of the std::string and this causes undefined behavour crashing the program because I suppose std::string::~string() doesn't nullify the pointer to the buffer and so when std::string::~string() tries to release the buffer for the second time with the same address this leads to famous double-free.

提交回复
热议问题