0
T** class_array
/* snip: malloc/creating classes */
delete[] class_array

Will the delete[] call correctly free class_array (an array of class pointers) and the requisite destructors?

1 Answers1

0

No, it will not.

The following is maybe an illustrative example along the lines of what I think you are trying to do. There is no need for an explicit delete, it's performed by arr_of_arr's destructor.

#include <iostream>
#include <memory>

using std::unique_ptr;
using std::cout;

class A
  {
  public:
    A() { my_cnt = cnt++; cout << "cons " << my_cnt << '\n'; }
    ~A() { cout << "kill " << my_cnt << '\n'; }

  private:
    unsigned my_cnt;
    static unsigned cnt;
  };

unsigned A::cnt;

int main()
  {
    unique_ptr<unique_ptr<A []> []> arr_of_arr(new unique_ptr<A []>[3]);

    arr_of_arr[0] = unique_ptr<A []>(new A[1]);
    arr_of_arr[1] = unique_ptr<A []>(new A[2]);
    arr_of_arr[2] = unique_ptr<A []>(new A[3]);

    return(0);
  }
WaltK
  • 724
  • 4
  • 13