please help me...

P

Pat

Hi,
I want to use "new" to build an array test[1000][1000]. Can you tell me how
to do?

Thank.

Pat
 
A

AirPete

Pat said:
I don't want to use "int test[1000][1000]".

Thanks.
Pat said:
Hi,
I want to use "new" to build an array test[1000][1000]. Can you tell
me how to do?

Thank.

Pat

Try a jagged vector of vectors:

(not compiled)

vector<vector<int> > arr;
arr.resize(1000);
for(int i =0;i<1000;++i)
arr.resize(1000);

Then simply:
arr[x][y] = 1234;

- Pete
 
O

osmium

Pat said:
I want to use "new" to build an array test[1000][1000]. Can you tell me how
to do?

This works.

<add boilerplate>

void foo(int b[][4])
{
cout << b[2][3] << endl;
}

void test()
{
int(*a)[4] = new int[3][4];
a[2][3] = 1024;
foo(a);
}
 
T

TJB

Pat said:
Hi,
I want to use "new" to build an array test[1000][1000]. Can you tell me how
to do?

Thank.

Pat
Hi,

You have to create an array of arrays like this:

int i,**test;

test = new int*[1000];
for (i = 0; i < 1000; i++)
test = new int[1000];

You could also start with an array of pointers, int* test[1000], and
initialize the elements in it.
 
A

Alf P. Steinbach

* "TJB said:
Pat said:
I want to use "new" to build an array test[1000][1000]. Can you tell me
how to do?

You have to create an array of arrays like this:

int i,**test;

test = new int*[1000];
for (i = 0; i < 1000; i++)
test = new int[1000];

You could also start with an array of pointers, int* test[1000], and
initialize the elements in it.


Well no, e doesn't "have to".

Except if e _really_ doesn't want a two-dimensional array.

See the answer from osmium earlier in the thread about how to allocate
a two-dimensional array using new,

int (*p)[1000] = new int[1000][1000];

The general question is also a FAQ, see
<url: http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.15>,
"How do I allocate a multidimensional array using new?"

Unfortunately, in spite of the heading the FAQ does not show how to allocate
a multidimensional array using new (!), only jagged arrays like yours. But
it does go on to show a few generally much better ways.
 
L

LNK2005

osmium said:
Pat writes:
int(*a)[4] = new int[3][4];

Maybe you should point out that this only works if the number of items in
the second dimension (=4) is known at compile time, which limits the
usefullness of this method.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,164
Messages
2,570,898
Members
47,439
Latest member
shasuze

Latest Threads

Top