Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Is this code correct? (http://www.velocityreviews.com/forums/t284717-is-this-code-correct.html)

highli 07-30-2004 03:13 PM

Is this code correct?
 
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!





Victor Bazarov 07-30-2004 03:27 PM

Re: Is this code correct?
 
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

Gianni Mariani 07-30-2004 03:39 PM

Re: Is this code correct?
 
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

Rolf Magnus 07-30-2004 03:51 PM

Re: Is this code correct?
 
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.

Victor Bazarov 07-30-2004 03:54 PM

Re: Is this code correct?
 
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.


marbac 07-30-2004 03:57 PM

Re: Is this code correct?
 
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

Gianni Mariani 07-30-2004 04:02 PM

Re: Is this code correct?
 
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

Rolf Magnus 07-30-2004 04:10 PM

Re: Is this code correct?
 
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.



All times are GMT. The time now is 12:09 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.