c++ - what will happen to the alloacted memory in a constructor if I throw exception from constructor? -
this question has answer here:
if allocating memory pointer using new in constructor , if throw exception after allocating memory form constructor, memory reclaimed or memory leak?
class { public: a() { p = new int(4); throw 1; } ~a() { delete p; } private: int *p; };
the memory not reclaimed. if correctly had destructor (and copy/assignment operator) still leak memory.
any constructed sub-objects destructed after throw. in case p
destructed, not confused memory pointed p
being free: 2 different things , memory in fact leaked.
Comments
Post a Comment