我最近发布了一个关于此的问题,但这是一个不同的问题.我使用动态内存分配创建了一个2D数组,在使用矩阵之后,我们需要通过删除来释放内存,我不明白为什么我们不能用delete [] matrix它来删除它而不是下面代码中的方法
int **matrix;
// dynamically allocate an array
matrix = new int *[row];
for (int count = 0; count < row; count++)
matrix[count] = new int[col];
// free dynamically allocated memory
for( int i = 0 ; i < *row ; i++ )
{
delete [] matrix[i] ;
delete [] matrix ;
}
Run Code Online (Sandbox Code Playgroud)
因为问题是因为在main()我创建了一个2D数组并使用其他int **函数分配值,我不知道如何删除分配的内存,循环将导致运行时错误
int main()
{
int **matrixA = 0, **matrixB = 0, **matrixResult = 0; // dynamically allocate an …Run Code Online (Sandbox Code Playgroud)