Dynamic double dimension
-
I am using the following code to allocate dynamic double dimensional array double **ImagePro; try { ImagePro = new double*[width]; for(int i = 0; i < height; i++) { ImagePro[i] = new double[width]; } } catch(CMemoryException* ex) { ImagePro = NULL; ex->ReportError(); return FALSE; } and for deletion for(int i = 0; i < height; i++) { delete ImagePro[i]; } delete ImagePro; return TRUE; My problem is whenever the height is greater than width I get assertion error message Pls Help
-
I am using the following code to allocate dynamic double dimensional array double **ImagePro; try { ImagePro = new double*[width]; for(int i = 0; i < height; i++) { ImagePro[i] = new double[width]; } } catch(CMemoryException* ex) { ImagePro = NULL; ex->ReportError(); return FALSE; } and for deletion for(int i = 0; i < height; i++) { delete ImagePro[i]; } delete ImagePro; return TRUE; My problem is whenever the height is greater than width I get assertion error message Pls Help
-
I am using the following code to allocate dynamic double dimensional array double **ImagePro; try { ImagePro = new double*[width]; for(int i = 0; i < height; i++) { ImagePro[i] = new double[width]; } } catch(CMemoryException* ex) { ImagePro = NULL; ex->ReportError(); return FALSE; } and for deletion for(int i = 0; i < height; i++) { delete ImagePro[i]; } delete ImagePro; return TRUE; My problem is whenever the height is greater than width I get assertion error message Pls Help
Did you try this ?
for(int i = 0; i < height; i++) { delete**[]** ImagePro[i]; } delete**[]** ImagePro;
And wich part of the code is asserting ? -
Did you try this ?
for(int i = 0; i < height; i++) { delete**[]** ImagePro[i]; } delete**[]** ImagePro;
And wich part of the code is asserting ? -
You should, since delete ImagePro[0]; Is the same as delete ImagePro; So when you the delete ImagePro, its already deleted.
-
It works fine when the height and width are equal or if the height is less than width The error occurs only when the height is greater than the width. BoundsChecker displays an error message "Dynamic memory overrun"
Hello. See my earlier post. You allocate no of items twice. You don't use , except in the allocating loop. Since you allocate room for items in your table. So it's natural that you go out of bounds if height is bigger than what you allocated (which is items). Kakan
-
Hello. See my earlier post. You allocate no of items twice. You don't use , except in the allocating loop. Since you allocate room for items in your table. So it's natural that you go out of bounds if height is bigger than what you allocated (which is items). Kakan
-
so how should I allocate and delete dynamic memory when the width and height are different. help me with some code.
-
so how should I allocate and delete dynamic memory when the width and height are different. help me with some code.
Hello again. I couldn't let go of your problem. :) I found a lot of suggestions at experts exhange, at: http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_21442835.html[^] This example looks interesting, and complete:
template < typename T > T **Allocate2DArray( int nRows, int nCols) { T **ppi; T *pool; T *curPtr; //(step 1) allocate memory for array of elements of column ppi = new T*[nRows]; //(step 2) allocate memory for array of elements of each row pool = new T [nRows * nCols]; // Now point the pointers in the right place curPtr = pool; for( int i = 0; i < nRows; i++) { *(ppi + i) = curPtr; curPtr += nCols; } return ppi; } template < typename T > void Free2DArray(T** Array) { delete [] *Array; delete [] Array; } int main() { double **d = Allocate2DArray(10000, 10000); d[0][0] = 10.0; d[1][1] = 20.0; d[9999][9999] = 2345.09; Free2DArray(d); }
In the main function, you can see how to use the array. I think you should try it out. Regards Kakan -
I am using the following code to allocate dynamic double dimensional array double **ImagePro; try { ImagePro = new double*[width]; for(int i = 0; i < height; i++) { ImagePro[i] = new double[width]; } } catch(CMemoryException* ex) { ImagePro = NULL; ex->ReportError(); return FALSE; } and for deletion for(int i = 0; i < height; i++) { delete ImagePro[i]; } delete ImagePro; return TRUE; My problem is whenever the height is greater than width I get assertion error message Pls Help
Arrun wrote:
ImagePro = new double*[width]; for(int i = 0; i < height; i++)
If you allocate room for
width
double pointers, it only makes sense that thefor
loop execute only that many times. Ifheight
happens to be larger thanwidth
, an error should be expected. See here for an example.Arrun wrote:
for(int i = 0; i < height; i++) { delete ImagePro[i]; } delete ImagePro;
Change to:
for(int i = 0; i < height; i++)
delete [] ImagePro[i];delete [] ImagePro;
"Take only what you need and leave the land as you found it." - Native American Proverb