One way to allocate dynamically a
by
array (of doubles,
say) is to first allocate space for
pointers to double and
then allocate
doubles for each of these:
double **myArray;
int i;
myArray = (double **) malloc(m*sizeof(double *));
assert(myArray);
for (i=0;i<m;i++) {
myArray[i] = (double *) malloc(n*sizeof(double));
assert(myArray[i]);
}
DoSomethingWith(myArray);
for (i=0;i<m;i++)
free((void *) myArray[i]);
free((void *) myArray);
This naturally can be extended to any number of dimensions. Perhaps a
better way is to simply allocate a single
by
block of memory
of the appropriate storage type and access the elements using
row-column formulae like
, where
runs from 0 to
and
runs from 0 to
.