Why “delete [][]… multiDimensionalArray;” operator in C++ does not exist

后端 未结 9 981
清酒与你
清酒与你 2021-02-05 06:09

I was always wondering if there is operator for deleting multi dimensional arrays in the standard C++ language.

If we have created a pointer to a single dimensional arra

9条回答
  •  清酒与你
    2021-02-05 06:43

    Because there is no way to call

    int **array = new int[dim1][dim2];
    

    All news/deletes must be balanced, so there's no point to a delete [][] operator.

    new int[dim1][dim2] returns a pointer to an array of size dim1 of type int[dim2]. So dim2 must be a compile time constant. This is similar to allocating multi-dimensional arrays on the stack.

提交回复
热议问题