Exceptions to the removal of indicators in c+



  • There is a need to find a way to ensure that, in any way possible, the programme is implemented (including possible exceptions) for each completed programme new implemented delete (sighs) delete was not carried out for anything. Used design:

    bool *a = new bool[n];
    int *k = new int[m];
    

    try {
    // сам код
    }
    catch(...) {
    delete [] a;
    delete [] k;
    }

    It's a problem.

    1. If, for example, new On line one, I'll call for an exception.
      delete for an uninitiated indicator.
    2. If delete In the penultimate line, there's an exception.
      I will not release the rest of the memory.

    How do you fix that?



  • If you call. new There will be no exception, no appropriation, so the signer will be nullptr♪ Statutory C++delete nullptr It doesn't do anything, it works correctly. So...

    Type my_ugly_pointer=nullptr;
    try
    { 
        my_ugly_pointer=new ...
    }
    catch(...)
    {
    }
    delete my_ugly_pointer;
    

Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2