445,820 Members | 1,187 Online
Need help? Post your question and get tips & solutions from a community of 445,820 IT Pros & Developers. It's quick & easy.

# Is this code correct?

 P: n/a Array2D = new (int*)[M]; for(int i = 0; i < M; ++i) Array2D[i] = new int[N]; for(int i = 0; i < M; ++i) delete [] Array2D[n]; delete [] Array2D; What is meaning of "(int*)[M]"? Is it necessary to "delete [] Array2D;", or just "delete Array2D;"? Thanks in advance! Jul 22 '05 #1
7 Replies

 P: n/a highli wrote: Array2D = new (int*)[M]; Huh? Shouldn't this be Array2D = new int*[M]; ? for(int i = 0; i < M; ++i) Array2D[i] = new int[N]; for(int i = 0; i < M; ++i) delete [] Array2D[n]; delete [] Array2D; This is apparently a fragment. As with every fragment, the judgment of its correctness depends highly on what it's a fragment of. What is meaning of "(int*)[M]"? It's a syntax error. If you need to get the meaning, you should ask somebody who wrote it. Is it necessary to "delete [] Array2D;", or just "delete Array2D;"? Yes, considering that you remove the parentheses from the first 'new' expression. Victor Jul 22 '05 #2

 P: n/a highli wrote: Array2D = new (int*)[M]; for(int i = 0; i < M; ++i) Array2D[i] = new int[N]; for(int i = 0; i < M; ++i) delete [] Array2D[n]; delete [] Array2D; This code is correct (although I would write it differenly) What is meaning of "(int*)[M]"? It's a type - an array of M pointers to int. Is it necessary to "delete [] Array2D;", or just "delete Array2D;"? It *is* neccessary. This is how I would do it though. http://tinyurl.com/65yv5 Jul 22 '05 #3

 P: n/a highli wrote: Array2D = new (int*)[M]; for(int i = 0; i < M; ++i) Array2D[i] = new int[N]; for(int i = 0; i < M; ++i) delete [] Array2D[n]; delete [] Array2D[i]; delete [] Array2D; What is meaning of "(int*)[M]"? M-Element array of pointer to int. Is it necessary to "delete [] Array2D;", or just "delete Array2D;"? Whatever you allocated with new[] has to be deallocated with delete[], so yes, you need delete[] here. Jul 22 '05 #4

 P: n/a Gianni Mariani wrote: highli wrote: Array2D = new (int*)[M]; for(int i = 0; i < M; ++i) Array2D[i] = new int[N]; for(int i = 0; i < M; ++i) delete [] Array2D[n]; delete [] Array2D; This code is correct (although I would write it differenly) What is meaning of "(int*)[M]"? It's a type - an array of M pointers to int. Does that mean that the parentheses are unnecessary? I would think that they are actually interfering with the type-id that needs to be there... It doesn't seem to work in VC++ v 7.1.. Is it necessary to "delete [] Array2D;", or just "delete Array2D;"? It *is* neccessary. Jul 22 '05 #5

 P: n/a 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;ifor(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 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 Jul 22 '05 #6

 P: n/a Victor Bazarov wrote: Gianni Mariani wrote: ... What is meaning of "(int*)[M]"? It's a type - an array of M pointers to int. Does that mean that the parentheses are unnecessary? I would think that they are actually interfering with the type-id that needs to be there... It doesn't seem to work in VC++ v 7.1.. I overlooked the "()" - it is wrong. I suspect the OP meant as you pointed out in the other post. new int*[M] It's interesting that GCC actually gives an interesting message. xxx.cpp: In function `int main()': xxx.cpp:7: error: array bound forbidden after parenthesized type-id xxx.cpp:7: note: try removing the parentheses around the type-id Jul 22 '05 #7

 P: n/a Gianni Mariani wrote: I suspect the OP meant as you pointed out in the other post. new int*[M] It's interesting that GCC actually gives an interesting message. I'd be surprised if an interesting message would not be interesting ;-) xxx.cpp: In function `int main()': xxx.cpp:7: error: array bound forbidden after parenthesized type-id xxx.cpp:7: note: try removing the parentheses around the type-id So gcc seems to have understood what you wanted to do, but rejects it because it's not allowed. Jul 22 '05 #8

### This discussion thread is closed

Replies have been disabled for this discussion.