PROBLEM: fzjaffe@hamlet.ucdavis.edu (Rory Jaffe), 16 Jul 92
I have declared:
double multiarray[][1000] ;
and now want to allocate storage. What is the proper syntax for a 'new'
statement allocating space for a [16][1000] array? The C++ faq doesn't
seem to have anything on multidimensional arrays.
.............................................................................
RESPONSE: stefans@informatik.unibw-muenchen.de (Stefan Schwarz BauV App. 3407)
16 Jul 92
Try this:
[ There may be a problem with the code below. I don't believe that
sizeof(double*) should be in the first new expression nor
sizeof(double) in the second new expression. -adc ]
double **array = new double* [sizeof(double*) * rows];
array[0] = new double [sizeof(double) * rows * columns];
for (int i = 1; i < rows; i++)
array = array[0] + i * columns;
Where array is list of pointers to the beginning of each row.
Now you can just write array[j] to reference the elements.
.............................................................................
RESPONSE: maxtal@extro.ucc.su.OZ.AU (John (MAX) Skaller)
typedef double dmill[1000];
dmill *multiarray=new dmill[16];
.............................................................................
RESPONSE: fzjaffe@hamlet.ucdavis.edu (Rory Jaffe)
The final solution to my problem was:
double (*mutiDimArrayPtr)[1000];
.
.
.
mutiDimArrayPtr = new double[16][1000] ;
p.s. I think some info on multidimensional arrays belongs in the FAQ.
This stuff is not simple!
.............................................................................
RESPONSE: maxtal@extro.ucc.su.OZ.AU (John MAX Skaller)
Never try something complicated when you can simplify it.
NEVER instantiate multidimeniosnal arrays.
USE A TYPEDEF.
typdef int i3[3];
typdef i3 i35[5];
i35*p=new i35[7];
No confusion here. There are 7 i35 objects, each is 5 i3 objects
each of which is an array of 3 integers.