This question has already been answered:
- Deleting pointers c ++ 2 response
It is necessary to find a way so that for any possible path of the program execution (including possible exceptions) for each executed new executed delete (and delete not executed for anything superfluous). I use a design:
bool *a = new bool[n]; int *k = new int[m]; try { // сам код } catch(...) { delete [] a; delete [] k; } Faced a problem:
- If, for example,
newon the first line throws an exception, then I will calldeletefor an uninitialized pointer. - If
deleteon thedeleteto-last line throws an exception, I will not release the rest of the allocated memory.
How to fix it?
std::vectorinstead of manual memory management. In addition, forboolsize will be ~ 8 times smaller than the original version. - αλεχολυτ