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

后端 未结 9 988
清酒与你
清酒与你 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:35

    The reason you have to loop, like in the example you mention, is that the number of arrays that needs to be deleted is not known to the compiler / allocator.

    When you allocated your two-dimensional array, you really created N one-dimensional arrays. Now each of those have to be deleted, but the system does not know how many of them there are. The size of the top-level array, i.e. the array of pointers to your second-level arrays, is just like any other array in C: its size is not stored by the system.

    Therefore, there is no way to implement delete [][] as you describe (without changing the language significantly).

提交回复
热议问题