473,385 Members | 1,676 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,385 software developers and data experts.

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 7967

"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*sizeof(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.newshosting.co m...

"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
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....
8
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). ...
6
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
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...
6
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...
7
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...
0
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...
9
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...
9
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...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: 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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...
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...

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.