无法删除动态分配的多维数组 [英] Can't delete dynamically allocated multidimensional array
本文介绍了无法删除动态分配的多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我无法删除动态生成的数组。有我如何创建它们:
I can't delete the dynamically generated arrays. There is how I create them:
template <typename T>
T **AllocateDynamic2DArray(int nRows, int nCols){
T **dynamicArray;
dynamicArray = new T*[nRows];
for( int i = 0 ; i < nRows ; i++ ){
dynamicArray[i] = new T[nCols];
for ( int j=0; j<nCols;j++){
dynamicArray[i][j]= 0;
}
}
return dynamicArray;
}
我用一个初始化一个二维数组:
And I initialize a 2D array using:
long double** h = AllocateDynamic2DArray<long double>(KrylovDim+1,KrylovDim);
我无法删除它。以下是我尝试的变体:
I can't delete it. Here are the variations that I've tried:
delete[] h;
并且它提供错误:不能删除不是指针的对象 / p>
and it gives the error: "cannot delete objects that are not pointers" when I apply this:
for (int qq=0; qq < KrylovDim+1; qq++){
for (int ww=0; ww < KrylovDim; ww++){
delete [] h[qq][ww];
}
delete [] h[qq];
}
有没有办法干净的删除?
Is there any way for a clean delete? I'm using visual studio 2010 if it helps.
推荐答案
请尝试以下操作:
for (int qq=0; qq < KrylovDim + 1; qq++)
{
delete [] h[qq];
}
delete [] h;
所以基本上你正在做相反的分配过程
So basically you are doing the reverse of the allocation process
for( int i = 0 ; i < nRows ; i++ ){
dynamicArray[i] = new T[nCols]; // Instead of allocating now delete !
这篇关于无法删除动态分配的多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文