Is this code correct?

H

highli

Array2D = new (int*)[M];

for(int i = 0; i < M; ++i)
Array2D = 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!
 
V

Victor Bazarov

highli said:
Array2D = new (int*)[M];

Huh? Shouldn't this be

Array2D = new int*[M];

?
for(int i = 0; i < M; ++i)
Array2D = 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
 
G

Gianni Mariani

highli said:
Array2D = new (int*)[M];

for(int i = 0; i < M; ++i)
Array2D = 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
 
R

Rolf Magnus

highli said:
Array2D = new (int*)[M];

for(int i = 0; i < M; ++i)
Array2D = new int[N];


for(int i = 0; i < M; ++i)
delete [] Array2D[n];


delete [] Array2D;
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.
 
V

Victor Bazarov

Gianni said:
highli said:
Array2D = new (int*)[M];

for(int i = 0; i < M; ++i)
Array2D = 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.
 
M

marbac

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 =new int ; //> Array2D = new int[N];

mat[j]=12;

for (int i=0;i<z;i++) //> for(int i = 0; i < M; ++i)
delete [] mat;//> delete [] Array2D[n]; <- i!!!!

delete [] mat; //> delete [] Array2D;
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 = new int[N];


for(int i = 0; i < M; ++i)
delete [] Array2D[n];


"delete [] Array2D[n];" Has to be "delete [] Array2D;"
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
 
G

Gianni Mariani

Victor said:
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
 
R

Rolf Magnus

Gianni said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top