dynamic multidimmensional arrays

R

RR

Hello I am trying to declare in my object something like

double **ma;

then do something like

ma = new int[x][y];

so I just want to declare a pointer to a double array and then
initialize the double array but I keep getting compiler error if
I do the above.

Is there an easy to do this
 
R

Russell Hanneken

RR said:
Hello I am trying to declare in my object something like

double **ma;

then do something like

ma = new int[x][y];

so I just want to declare a pointer to a double array and then
initialize the double array but I keep getting compiler error if
I do the above.

This is actually addressed in the FAQ:

[16.15] How do I allocate multidimensional arrays using new?
http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.15

Check out the questions following 16.15 too.
 
S

Silviu Minut

ma = new double*[100]; // 100 elements of type double*
for(i=0; i<100; i++)
ma=new double[200]; // each ma is an array

This is all clear if you think of

double ** ma;

as (double *) * ma. This means ma is a 1D array of type double*,
so each ma will be a double*.

You allocate ma as you allocate any 1D array, with the appropriate
type (in this case (double*)). Then you allocate each ma.

Note that this way, ma is not contiguous (each row ma is though).
If you want to make it contiguous, you still do

ma = new double*[100];

then you allocate the first row to have the size of all rows combined:

ma[0]=new double [100*200]; // all of ma is contiguous.

then you make each pointer ma point where the i-th row should start
in ma[0]:

for (i=1; i<100; i++)
ma=ma[i-1]+200;
 
G

Gianni Mariani

RR said:
Hello I am trying to declare in my object something like

double **ma;

then do something like

ma = new int[x][y];

so I just want to declare a pointer to a double array and then
initialize the double array but I keep getting compiler error if
I do the above.

Is there an easy to do this



If you know one dimension is fixed in size then this works.

double (*ma)[229];

void f()
{
ma = new double[ 110 ][ 229 ];
}
 
M

Micah Cowan

RR said:
Hello I am trying to declare in my object something like

double **ma;

then do something like

ma = new int[x][y];

so I just want to declare a pointer to a double array and then
initialize the double array but I keep getting compiler error if
I do the above.

Is there an easy to do this

In the example above, you are neither declaring a pointer to a
double array, nor are you initializing anything with a double
array: you are trying to initialize a
pointer-to-pointer-to-double with an array-of-arrays-of-int. They
have pretty much nothing at all in common, and one certainly
isn't convertable to the other.

Part of your problem above may have just been to accidentally
type "int" where you meant "double"; however, it's still
important to realize that arrays and pointers are still *very*
different types. Don't let anyone try to tell you that arrays are
really pointers--that's what probably led you to this confusion
in the first place.

Even if the above had been:

double **ma = new double[x][y];

It'd still fail to compile. A (double [][]) can't be assigned to
a (double**): you wouldn't even be able to assign a (double [])
to a (double *), where it not the fact that an array expression
can be implicitly converted to a pointer to the first element of
an array. But that implicit conversion applied to a (double [][])
results in a (double (*)[]) [that is, a
pointer-to-array-of-doubles]... which is absolutely *not* the
same as a pointer-to-pointer-to-double.

So go get a good C++ book (Stroustrup, for example), and study
the difference between arrays and pointers until you are certain
you understand the difference.

If y is a constant, you could do:

double (*ma)[y];

followed by (or initialized similarly to):

ma = new double[x][y];

But I have a nagging suspicion that whatever you are doing may
not be the best way to address the problem you are building this
particular solution for. So: what is the context? What are you
trying to accomplish?
 

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,145
Messages
2,570,826
Members
47,372
Latest member
LucretiaFo

Latest Threads

Top