C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

How to new a two-dimension array of int

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Larry Lindsey
Guest





PostPosted: Wed Sep 24, 2003 7:34 pm    Post subject: Re: How to new a two-dimension array of int Reply with quote




"Wonder" <wonder (AT) wonder (DOT) com> wrote

Quote:
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 (*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



Back to top
Wonder
Guest





PostPosted: Wed Sep 24, 2003 7:36 pm    Post subject: How to new a two-dimension array of int Reply with quote



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.



Back to top
Ron Natalie
Guest





PostPosted: Wed Sep 24, 2003 7:39 pm    Post subject: Re: How to new a two-dimension array of int Reply with quote




"Wonder" <wonder (AT) wonder (DOT) com> wrote

Quote:
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.

Quote:
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?



Back to top
Ron Natalie
Guest





PostPosted: Wed Sep 24, 2003 7:41 pm    Post subject: Re: How to new a two-dimension array of int Reply with quote


"Larry Lindsey" <gtg306b (AT) prism (DOT) gatech.edu> wrote

Quote:


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.




Back to top
Larry Lindsey
Guest





PostPosted: Wed Sep 24, 2003 7:47 pm    Post subject: Re: How to new a two-dimension array of int Reply with quote


"Ron Natalie" <ron (AT) sensor (DOT) com> wrote

Quote:

"Larry Lindsey" <gtg306b (AT) prism (DOT) gatech.edu> wrote



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.



Back to top
Mike Wahler
Guest





PostPosted: Sat Sep 27, 2003 12:20 am    Post subject: Re: How to new a two-dimension array of int Reply with quote

"Larry Lindsey" <gtg306b (AT) prism (DOT) gatech.edu> wrote

Quote:

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

So why do you purport to teach others?

-Mike



Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.