For some reason I think I have allRates is x*y*z length pointer to doubles and rates is essentially a 3dimensional array. But I can’t recall exactly how this works.
// Allocate 3D Rate Array
double *allRates = malloc( x*y*z*sizeof(double) );
if (!allRates) exit(1);
double ***rates = malloc( x*sizeof(double **) );
if (!rates) exit(1);
for(i=0; i<x; i++) {
rates[i] = malloc(y * sizeof(double *));
// Check rates[i] allocation?
for(j=0; j<y; j++) {
rates[i][j] = allRates + (i*y*z) + (j*z);
}
}
It runs properly… I’m just working on documentation and haven’t worked on this part of the code since March.
allocates a block of memory large enough to hold
x*y*zvalues of typedouble(if the dimensions are small enough; if the mathematical result of the product isn’t representable as asize_t, it allocates the remainder of that moduloSIZE_MAX + 1).allocates a block of memory large enough to hold
xvalues of typedouble**(again, ifxis small enough). Those are used as the indices for the first dimension.Checking the allocation is definitely advisable. If nothing fails, each
rates[i]is made to point to a block of memory large enough to holdypointers todouble.Each of the
double*srates[i][j]is made to point into the block allocated toallRates, at an offset ofi*(y*z) + j*zelements,itimes the size of any×z-plane plusjtimes the length of az-element row, so thatrates[i][j]points to the first element of rowjin planei.If C99 is available, or
yandzare compile-time constants, that would be simpler achieved by allocatingwith fewer indirections when addressing.