delete multidimensional arrays

后端 未结 5 1954
醉梦人生
醉梦人生 2021-01-24 00:43

In C++ FAQ, the [16.16] gives the following example,

void manipulateArray(unsigned nrows, unsigned ncols[])
{
   typedef Fred* FredPtr;
   FredPtr* matrix = new          


        
5条回答
  •  猫巷女王i
    2021-01-24 01:18

    What you're missing is the horribly evil indentation.

    delete matrix[i-1]; happens once per loop iteration and deletes the nested arrays. delete matrix happens just one time after the loop completes and deletes the outer array.

    Never write code like this in C++, use vector > instead.

    The reason the deletes also exist in the catch is because if you catch an exception you're still responsible to clean up the memory you allocated.

提交回复
热议问题