how to "new" a two-dimension array in C++?

J

James

Hi,

I want to directly create a dynamic two-dimension double array, i.e.
double pp[][]. I found the "new" is only for one-dimension array, i.e.
double *p = new p[dynamic_size]. How to "new" a two-dimension array?
Is the only way to create a class?

In java, I can easily create a two-dimension array by "double[][] pp =
new double[dynamic_size][dynamic_size]". But, how to do it in C++?

Looking forward to hearing from you!

Regards,

James
 
J

Joona I Palaste

James said:
I want to directly create a dynamic two-dimension double array, i.e.
double pp[][]. I found the "new" is only for one-dimension array, i.e.
double *p = new p[dynamic_size]. How to "new" a two-dimension array?
Is the only way to create a class?
In java, I can easily create a two-dimension array by "double[][] pp =
new double[dynamic_size][dynamic_size]". But, how to do it in C++?
Looking forward to hearing from you!

Perhaps you should ask C++ questions in comp.lang.c++?
 
O

Old Wolf

In java, I can easily create a two-dimension array by "double[][] pp =
new double[dynamic_size][dynamic_size]". But, how to do it in C++?

In C (as well as C++) you must know all, or all-but-one,
of an array's dimensions at compile time.

In C you should create a one-dimensional array with malloc()
and then you can use macros to refer to it as if it were 2-d.

In C++ there are other solutions, you should post to comp.lang.c++
to get answers on that.
 
E

E. Robert Tisdale

James said:
I want to directly create a dynamic two-dimension double array, i.e.
double pp[][];
I found the "new" is only for one-dimension array, i.e.
double *p = new p[dynamic_size];
How to "new" a two-dimension array?
Is the only way to create a class?

In java, I can easily create a two-dimension array by
double[][] pp = new double[dynamic_size][dynamic_size];
But, how to do it in C++?
Looking forward to hearing from you!

Forget about new and try this:

double pp[dynamic_size][dynamic_size];

Variable length arrays are included in the C99 standard
and will be included in the new C++ standard.
Your compiler probably supports them already.
 

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

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top