delete multidimensional arrays

后端 未结 5 1990
醉梦人生
醉梦人生 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条回答
  •  我在风中等你
    2021-01-24 01:19

    When you in a loop deleting each of the rows, you're freeing up the memory allocated to the corresponding row. Then you need to free up the memory allocated for the pointers to each row.

    Think of it this way:

    FredPtr* matrix = new FredPtr[nrows];
    

    allocates an array of pointers to rows - and it will need to be freed up at the end.

    Then for each of the rows,

    matrix[i] = new Fred[ ncols[i] ];
    

    allocates memory for an array of pointers to columns - and it will need to be freed up separately.

提交回复
热议问题