在C ++中的2D动态内存分配数组中自由分配内存 [英] free allocated memory in 2D dynamic memory allocation array in C++
问题描述
我最近发布了一个有关这个问题,但这是一个不同的问题。我使用动态内存分配创建了一个二维数组,使用矩阵后,我们需要通过删除它释放内存,我不明白为什么我们不能只使用 delete [] matrix
int ** matrix;
//动态分配数组
matrix = new int * [row];
for(int count = 0; count< row; count ++)
matrix [count] = new int [col]
//自由动态分配的内存
for(int i = 0; i <* row; i ++)
{
delete [] matrix [i]
delete [] matrix;因为问题是因为在 main() / code>我创建了一个二维数组并使用其他 int **
函数分配值,我不知道如何删除分配的内存,循环将导致运行时错误 int main()
{
int ** matrixA = 0,** matrixB = 0 ,** matrixResult = 0; //动态分配数组
int rowA,colA,rowB,colB; //保存矩阵的大小
//获取输入法的值
int inputMethod = userChoiceOfInput();
if(inputMethod == 1)//通过键盘选择输入
{
cout< 矩阵A输入... \\\
;
matrixA = getMatricesByKeyboard(& rowA,& colA);
cout<< 矩阵B输入... \\\
;
matrixB = getMatricesByKeyboard(& rowB,& colB);
}
else if(inputMethod == 2)//通过文件选择输入
{
matrixA = getMatricesByFileInput(F:\\matrixA.txt,& rowA,& colA);
matrixB = getMatricesByFileInput(F:\\matrixB.txt,& rowB,& colB);
}
//添加(matrixA,& rowA,& colA,matrixB,& rowB,& colB);
cout<< matrixA [1] [0];
////////////////////////运行时错误/////////////// ////////
//自由分配的矩阵A的内存
for(int i = 0; i {
delete [] matrixA [i];
delete [] matrixA;
}
//矩阵B的空闲分配内存
for(int i = 0; i {
delete [] matrixB [i ];
delete [] matrixB;
}
////////////////////////运行时错误////////////// /////////
//矩阵A的空闲分配内存
delete [] matrixA; //我不知道什么会这些删除
delete [] matrixB;
return 0;
}
解决方案矩阵并删除每个数组。在这样做的最后,你可以删除矩阵本身
//自由动态分配内存
for = 0; i <* row; i ++)
{
delete [] matrix [i] //删除矩阵中的数组
}
//删除实际矩阵
delete [] matrix;
I recently posted a question about this, but this is a different question. I created a 2D arrays using dynamic memory allocation, after the matrix is used, we need to free the memory by delete it and I dont understand why we cant just use delete [] matrix
to delete it instead of the method in the codes below
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 ;
}
Because the problem is because in main()
i created a 2D array and assign the values using other int **
functions, i dont know how to delete the allocated memory, the loop will cause runtime error
int main()
{
int **matrixA = 0, **matrixB = 0, **matrixResult = 0; // dynamically allocate an array
int rowA, colA, rowB, colB; // to hold the sizes of the matrices
// get values for input method
int inputMethod = userChoiceOfInput();
if (inputMethod == 1) // select input by keyboard
{
cout << "Matrix A inputting...\n";
matrixA = getMatricesByKeyboard(&rowA, &colA);
cout << "Matrix B inputting...\n";
matrixB = getMatricesByKeyboard(&rowB, &colB);
}
else if (inputMethod == 2) // select input by files
{
matrixA = getMatricesByFileInput("F:\\matrixA.txt", &rowA, &colA);
matrixB = getMatricesByFileInput("F:\\matrixB.txt", &rowB, &colB);
}
//addition(matrixA, &rowA, &colA, matrixB, &rowB, &colB);
cout << matrixA[1][0];
////////////////////////run time error///////////////////////
// free allocated memory of matrix A
for( int i = 0 ; i < rowA ; i++ )
{
delete [] matrixA[i] ;
delete [] matrixA ;
}
// free allocated memory of matrix B
for( int i = 0 ; i < rowB ; i++ )
{
delete [] matrixB[i] ;
delete [] matrixB ;
}
////////////////////////run time error///////////////////////
// free allocated memory of matrix A
delete [] matrixA ; // i dont know what would these delete
delete [] matrixB ;
return 0;
}
解决方案 You have to go through your matrix and delete each array. At the end of doing that you can delete the matrix itself
// free dynamically allocated memory
for( int i = 0 ; i < *row ; i++ )
{
delete[] matrix[i]; // delete array within matrix
}
// delete actual matrix
delete[] matrix;
这篇关于在C ++中的2D动态内存分配数组中自由分配内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!