Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 19th, 2005, 08:39 PM
RR
Guest
 
Posts: n/a
Default dynamic multidimmensional arrays

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
  #2  
Old July 19th, 2005, 08:39 PM
Russell Hanneken
Guest
 
Posts: n/a
Default Re: dynamic multidimmensional arrays

RR wrote:[color=blue]
> 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.[/color]

This is actually addressed in the FAQ:

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

Check out the questions following 16.15 too.

--
Russell Hanneken
rghanneken@pobox.com
Remove the 'g' from my address to send me mail.


  #3  
Old July 19th, 2005, 08:40 PM
Silviu Minut
Guest
 
Posts: n/a
Default Re: dynamic multidimmensional arrays

ma = new double*[100]; // 100 elements of type double*
for(i=0; i<100; i++)
ma[i]=new double[200]; // each ma[i] 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[i] 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[i].

Note that this way, ma is not contiguous (each row ma[i] 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[i] point where the i-th row should start
in ma[0]:

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





On Sat, 25 Oct 2003 17:43:58 -0400, RR wrote:
[color=blue]
> 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[/color]

  #4  
Old July 19th, 2005, 08:40 PM
Gianni Mariani
Guest
 
Posts: n/a
Default Re: dynamic multidimmensional arrays

RR wrote:[color=blue]
> 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[/color]



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

double (*ma)[229];

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

  #5  
Old July 19th, 2005, 08:46 PM
Micah Cowan
Guest
 
Posts: n/a
Default Re: dynamic multidimmensional arrays

"RR" <ergodicsum@yahoo.com> writes:
[color=blue]
> 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[/color]

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?

--
Micah J. Cowan
micah@cowan.name
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles