Hi,
I got following Version from a Book (2D Array):
int z=5, s=6;
int** mat=new int* [z]; //> int **Array2D; Array2D = new int* [M];
for (int i=0;i<z;i++) //>for(int i = 0; i < M; ++i)
mat [i]=new int [s]; //> Array2D[i] = new int[N];
mat[i][j]=12;
for (int i=0;i<z;i++) //> for(int i = 0; i < M; ++i)
delete [] mat[i];//> delete [] Array2D[n]; <- i!!!!
delete [] mat; //> delete [] Array2D;
highli wrote:
Array2D = new (int*)[M];
Remove the brackets (), looks like casting.
Make sure that Array2D is of type int**
for(int i = 0; i < M; ++i)
Array2D[i] = new int[N];
for(int i = 0; i < M; ++i)
delete [] Array2D[n];
"delete [] Array2D[n];" Has to be "delete [] Array2D[i];"
delete [] Array2D;
What is meaning of "(int*)[M]"?
The meaning of "Array2D = new int* [M];" is that you allocate M Pointers
of type int which are accessable by Array2D (**).
Is it necessary to "delete [] Array2D;", or just "delete Array2D;"?
Yes it is necessary, because you allocated an array (field of objects)
and not a single object.
Thanks in advance!
regards marbac