| View previous topic :: View next topic |
| Author |
Message |
Larry Lindsey Guest
|
Posted: Wed Sep 24, 2003 7:34 pm Post subject: Re: How to new a two-dimension array of int |
|
|
"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
|
Posted: Wed Sep 24, 2003 7:36 pm Post subject: 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.
|
|
| Back to top |
|
 |
Ron Natalie Guest
|
Posted: Wed Sep 24, 2003 7:39 pm Post subject: Re: How to new a two-dimension array of int |
|
|
"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
|
Posted: Wed Sep 24, 2003 7:41 pm Post subject: Re: How to new a two-dimension array of int |
|
|
"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
|
Posted: Wed Sep 24, 2003 7:47 pm Post subject: Re: How to new a two-dimension array of int |
|
|
"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
|
Posted: Sat Sep 27, 2003 12:20 am Post subject: Re: How to new a two-dimension array of int |
|
|
"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 |
|
 |
|