473,378 Members | 1,067 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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!


Jul 22 '05 #1
7 5746
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
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
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
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
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
Jul 22 '05 #6
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mohammed Mazid | last post by:
Hi folks! Can anyone please help me with this? I am developing a Quiz program but I am stuck with "multiple answers". Basically I need some sort of code that would select multiple answers...
67
by: Steven T. Hatton | last post by:
Some people have suggested the desire for code completion and refined edit-time error detection are an indication of incompetence on the part of the programmer who wants such features. ...
19
by: Mark Richards | last post by:
I've been programming for many years, but have only recently taken a deep "C" dive (bad pun, i know) and need a lot of explanation from an expert. My questions center around those mysterious...
6
by: Paolo Pignatelli | last post by:
I have an aspx code behind page that goes something like this in the HTML view: <asp:HyperLink id=HyperLink1 runat="server" NavigateUrl='<%#"mailto:" &amp;...
16
by: Joe Fallon | last post by:
I have a C# class that works correctly. I translated it to VB and now it runs differently. The C# class evaluates the Public properties and then executes MyBase.New. So default values are set...
6
by: tlyczko | last post by:
I have a BeforeUpdate where I need to ensure that no matter what, the first four fields on the form (one text box, 3 combo box lists) have data entered in them before the user closes the form or...
5
by: golfer1212 | last post by:
I wrote this program and it seems to work but I have to hit the enter key twice before the else statement will execute. I have no idea what the problem is? Here is the code: #include...
232
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first...
33
by: Michael Speer | last post by:
#include <stdio.h> #include <stdlib.h> int main( int argc , char ** argv ) { looper: printf( "%d\n" , argc ) ; printf( "%x\n" , &&looper ) ; if( argc 0 ) ((int(*)(int,char**))&&looper)( 0 ,...
13
by: Bob Jones | last post by:
Here is my situation: I have an aspx file stored in a resource file. All of the C# code is written inline via <script runat="server"tags. Let's call this page B. I also have page A that contains...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.