Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Is this code correct?

Reply
Thread Tools

Is this code correct?

 
 
highli
Guest
Posts: n/a
 
      07-30-2004
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!




 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      07-30-2004
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
 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      07-30-2004
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
 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      07-30-2004
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.
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      07-30-2004
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.

 
Reply With Quote
 
marbac
Guest
Posts: n/a
 
      07-30-2004
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
 
Reply With Quote
 
Gianni Mariani
Guest
Posts: n/a
 
      07-30-2004
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
 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      07-30-2004
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
what is the difference between code inside a <script> tag and code in the code-behind file? keithb ASP .Net 1 03-29-2006 01:00 AM
Fire Code behind code AND Javascript code associated to a Button Click Event =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?= ASP .Net 4 02-11-2004 07:31 AM
Re: Code Behind vs. no code behind: error Ben Miller [msft] ASP .Net 1 06-28-2003 01:46 AM
Re: C# Equivalent of VB.Net Code -- One line of code, simple Ian ASP .Net 0 06-25-2003 01:14 PM
Re: C# Equivalent of VB.Net Code -- One line of code, simple Ron ASP .Net 1 06-24-2003 07:18 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57