473,662 Members | 2,581 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to new a two-dimension array of int

For compiler g++ on Linux, it doesn't work if I use

int **data = new int[m][n];

Can I new an array of int* first, and new an one-dimension array for the
first int*,
then use a loop to make every int* point to the right position?

int **data = new int*[m];
data[0] = new int[m*n];
for(i = 1; i < m; i++)
data[i] = data[0] + i * n;

It should works, however, it failed in my MPI program.
Could anyone give me a better method? Thanks.

Jul 19 '05 #1
5 7985

"Wonder" <wo****@wonder. com> wrote in message news:3f******** @rutgers.edu...
For compiler g++ on Linux, it doesn't work if I use

int **data = new int[m][n];

Can I new an array of int* first, and new an one-dimension array for the
first int*,
then use a loop to make every int* point to the right position?

int **data = new int*[m];
data[0] = new int[m*n];
for(i = 1; i < m; i++)
data[i] = data[0] + i * n;

It should works, however, it failed in my MPI program.
Could anyone give me a better method? Thanks.

Use pointers. C++ requires you to instantiate arrays with const values. So
instead:
int **data = (int**)malloc(m *sizeof(int*));
for (i=0;i<m;i++)
(*data+i)=(int* )malloc(n*sizeo f(int));

That would be my take. This will work, but there will probably be better
ways of doing it.

--Larry
Jul 19 '05 #2

"Wonder" <wo****@wonder. com> wrote in message news:3f******** @rutgers.edu...
For compiler g++ on Linux, it doesn't work if I use

int **data = new int[m][n];
Because the type of new int[m][n] is not int**.
POINTERS ARE NOT THE SAME AS ARRAYS.
Can I new an array of int* first, and new an one-dimension array for the
first int*,
then use a loop to make every int* point to the right position?

int **data = new int*[m];
data[0] = new int[m*n];
for(i = 1; i < m; i++)
data[i] = data[0] + i * n;


This looks like it should have worked to me. What happened?
Jul 19 '05 #3

"Larry Lindsey" <gt*****@prism. gatech.edu> wrote in message news:bk******** **@news-int.gatech.edu. ..
Use pointers. C++ requires you to instantiate arrays with const values. So
instead:


He did use pointers. Why on earth are you using malloc? Your code won't
work anyhow. Presuably you meant *(data+i) or data[i] in the lhs of the
last line.

Jul 19 '05 #4

"Ron Natalie" <ro*@sensor.com > wrote in message
news:3f******** *************** @news.newshosti ng.com...

"Larry Lindsey" <gt*****@prism. gatech.edu> wrote in message

news:bk******** **@news-int.gatech.edu. ..

Use pointers. C++ requires you to instantiate arrays with const values. So instead:


He did use pointers. Why on earth are you using malloc? Your code won't
work anyhow. Presuably you meant *(data+i) or data[i] in the lhs of the
last line.


Oh, yeah. Good point. I'm basically a newbie anywho.
Jul 19 '05 #5
"Larry Lindsey" <gt*****@prism. gatech.edu> wrote in message
news:bk******** **@news-int.gatech.edu. ..

Oh, yeah. Good point.
I'm basically a newbie anywho.


So why do you purport to teach others?

-Mike
Jul 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1779
by: SimonC | last post by:
I'm looking to do something similar to a feature found on Ticketmaster.com, where you select your seats at a venue, and then you have two minutes in which to take or leave them. QUESTION 1a. Inside (or just after) the same query that searches for available seats, I need to SIMULTANEOUSLY mark those seats as "on hold". I've only read about, but not yet used MySQL transactions, and wonder if this simultaneous "search-and-hold"...
8
1742
by: John Grenier | last post by:
Hi, I have to determine the "standing" (WIN - TIE - LOSS) from confrontations between two teams on a contest. The table matchResults has fields cont_id, team_id and contest_result (int). TABLE matchResults cont_id team_id contest_result 1 1 3 1 2 5
6
1871
by: Willem | last post by:
Hi, I have a newbie question: is it possible to make a search form in asp that searches in two different databases (access)? Willem
10
9346
by: Hank1234 | last post by:
Can I use one Data Adapter and one Command Builder to update amny tables? Currently in my data adapter I query two tables and fill them into two tables in a data set. When I make a change to a record in the second table and call the update method of the data adapter the command builders update command text is for the first table. Can the command builder handle two tables? Code example: Dim oCOnn As New SqlConnection("Data Source=.;" &...
6
4072
by: Matt K. | last post by:
Hi there, I have a form in an Access project that contains a subform which displays the results of a query of the style "select * from where = #a certain date#". In the main part of the form the user can change the date, which will force a requery in the subform to bring up records from the date selected. My question is this... The query in the subform is a very simple one, with only three fields being returned. In the interest of...
7
12883
by: Prabhudhas Peter | last post by:
I have two object instances of a same class... and i assigned values in both object instances (or the values can be taken from databse and assigned to the members of the objects)... Now i want to compare these two objects so that it will return true if both object's members have the same value... it is good if u can give me a single function or simple code snippet.. Thank U -- Peter...
0
1684
by: clintonG | last post by:
I applied aspnet_regsql to SQL2K which was working fine throughout Beta 2 development. After installing Visual Studio and SQL Express RTM my application has blown up. Logging in to the application became realllllllllllly slow. Content in LoginView Role Groups was not displaying even after a user in a role had logged in. It was taking about 15 seconds or so for the login control to display when the login link was selected on the homepage....
9
5259
by: Steven | last post by:
Hello, I have a question about strcmp(). I have four words, who need to be compared if it were two strings. I tried adding the comparison values like '(strcmp(w1, w2) + strcmp(w3, w4))', where w1 and w2 make up the first string and, w3 and w4 make up the second string. I do not want to allocate memory, then put the words together to create a string only to facilitate strcmp() comparison. My question; Does anyone know how to get the...
9
2015
by: dhable | last post by:
I just started working with Python and ran into an annoyance. Is there a way to avoid having to use the "from xxx import yyy" syntax from files in the same directory? I'm sure it's been asked a million times, but I can't seem to find the answer. For example, I have two classes stored in separate files as such. File: one.py ======== class One:
0
8344
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8857
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8764
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8546
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7367
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5654
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4347
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2762
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1993
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.