c++ - Can't delete dynamically allocated multidimensional array -
i can't delete dynamically generated arrays. there how create them:
template <typename t> t **allocatedynamic2darray(int nrows, int ncols){ t **dynamicarray; dynamicarray = new t*[nrows]; for( int = 0 ; < nrows ; i++ ){ dynamicarray[i] = new t[ncols]; ( int j=0; j<ncols;j++){ dynamicarray[i][j]= 0; } } return dynamicarray; }
and initialize 2d array using:
long double** h = allocatedynamic2darray<long double>(krylovdim+1,krylovdim);
i can't delete it. here variations i've tried:
delete[] h;
and gives error: "cannot delete objects not pointers" when apply this:
(int qq=0; qq < krylovdim+1; qq++){ (int ww=0; ww < krylovdim; ww++){ delete [] h[qq][ww]; } delete [] h[qq]; }
is there way clean delete? i'm using visual studio 2010 if helps.
try this:
for (int qq=0; qq < krylovdim + 1; qq++) { delete [] h[qq]; } delete [] h;
so doing reverse of allocation process
for( int = 0 ; < nrows ; i++ ){ dynamicarray[i] = new t[ncols]; // instead of allocating delete !
Comments
Post a Comment