c++11 - how to handle double free crash in c++ -
deleting double pointer cause harmful effect crash program , programmer should try avoid not allowed. sometime if doing how take care of this. delete in c++ noexcept operator , it'll not throw exceptions. , written type void. how catch kind of exceptions. below code snippet
class myexception: public std::runtime_error { public: myexception(std::string const& msg): std::runtime_error(msg) { cout<<"inside class \n"; } }; void main() { int* set = new int[100]; cout <<"memory allcated \n"; //use set[] delete [] set; cout <<"after delete first \n"; try{ delete [] set; throw myexception("error while deleting data \n"); } catch(std::exception &e) { cout<<"exception \n"; } catch(...) { cout<<"generic catch \n"; } cout <<"after delete second \n";
in case tried catch exception no success. pleas provide input how we'll take care of these type of scenario. in advance!!!
given behaviour on subsequent delete[]
undefined, there's nothing can do, aside writing
set = nullptr;
immediately after first delete[]
. exploits fact deletion of nullptr
no-op.
but really, encourages programmers sloppy.
Comments
Post a Comment