This question has already been answered:

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:

  1. If, for example, new on the first line throws an exception, then I will call delete for an uninitialized pointer.
  2. If delete on the delete to-last line throws an exception, I will not release the rest of the allocated memory.

How to fix it?

Reported as a duplicate by the participants zRrr , cheops , user194374, aleksandr barakin , Nick Volynkin Jun 14 '16 at 8:19 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • four
    "To pass the task" - and you do not see another reason for this? - VladD
  • 3
    RAII basically do not want to use? - KoVadim
  • one
    Use std::vector instead of manual memory management. In addition, for bool size will be ~ 8 times smaller than the original version. - αλεχολυτ
  • @alexolut, something doesn’t look like a duplicate - I ’m not talking about exceptions ... - Qwertiy
  • @Qwertiy and where am I? :) - αλεχολυτ

4 answers 4

If there is an exception when calling new , no assignment will be made, so nullptr will be in the pointer. According to C++ , delete nullptr does nothing and executes correctly. So that:

 Type my_ugly_pointer=nullptr; try { my_ugly_pointer=new ... } catch(...) { } delete my_ugly_pointer; 
  • Uh, why in the code of the vehicle in the pointer will be nullptr? He seems to be not initialized? Or do you mean your code? - VladD
  • @VladD is my code, this is exactly what I have done. - gbg
  • Oh, I realized, sorry. - VladD
  • And what to do, if on one of the first delete the exception takes place? The rest will not be deleted - Parket
  • @Parket, you either use a smart pointer, or put blocks of exceptions with a matrioshka. - gbg

Shove your new into try.

https://stackoverflow.com/questions/27133056/what-happens-when-i-call-delete-on-an-uninitialized-pointer-in-c

In addition, RAII and smart pointers are at your service.

    You have already been advised to "use a smart pointer." Showing (C ++ option 14):

     #include <memory> using std::make_unique; try { auto pt = std::make_unique<Type>(); auto pt2 = std::make_unique<Type2>(); ... } catch(...) { } 

    If there is an exception in the pt2 constructor, pt will be destroyed by the smart pointer unique_ptr ;

    • one
      std::auto_ptr obsolete class. Do not use it. - αλεχολυτ
    • one
      And with std::unique_ptr it’s better to use std::make_unique . Less allocations, and duplication of the type name. - αλεχολυτ
    • 2
      std::make_unique is c++14 . And why produce answers? - αλεχολυτ
    • @alexolut a lot of people sitting on the 98 standard and nowhere else for 5 years from it. - gbg
    • one
      I am not talking about not mentioning the old Standard in principle, but about the need to do this in a separate answer. You can after all add to the existing and reduce the number of extra information on the page. Or is it a fight for "likes"? - αλεχολυτ

    You have already been advised to "use a smart pointer." I show (option C ++ 98):

     #include <memory> try { std::auto_ptr<Type> pt( new Type ); std::auto_ptr<Type> pt2( new Type ); ... } catch(...) { } 

    If there is an exception in the pt2 constructor, pt will be destroyed by the smart pointer auto_ptr ;