473,398 Members | 2,188 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,398 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 5750
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.