News:

This week IPhone 15 Pro winner is karn
You can be too a winner! Become the top poster of the week and win valuable prizes.  More details are You are not allowed to view links. Register or Login 

Main Menu

c++ arrays tips - Allocating multidimensional arrays

Started by ben2ong2, October 06, 2006, 04:52:49 PM

Previous topic - Next topic

0 Members and 2 Guests are viewing this topic.

ben2ong2

PROBLEM: You are not allowed to view links. Register or Login (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: You are not allowed to view links. Register or Login (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: You are not allowed to view links. Register or Login (John (MAX) Skaller)

typedef double dmill[1000];
dmill *multiarray=new dmill[16];

.............................................................................

RESPONSE: You are not allowed to view links. Register or Login (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: You are not allowed to view links. Register or Login (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.
You are not allowed to view links. Register or Login
You are not allowed to view links. Register or Login