86 static unsigned Dims[5] = { 1, 2, 3, 4, 5 };
87 double *****A, *B, val;
88 extern char *mallocNd();
91 A = (
double *****) mallocNd( 5, Dims,
sizeof(
double) );
94 fprintf( stderr,
"No memory!");
105 val = l + 10*(k + 10*(j + 10*i));
106 A[0][i][j][k][l] = val;
114 B = &(A[0][0][0][0][0]);
115 for(i=0; i<120; i++) {
116 printf(
"%6.0f ", B[i] );
117 if ((i %10) == 9) putchar(
'\n');
128 unsigned int sizeofelt;
131 char *ptr, *tableBase, *arrayBase, *nextFreeArray, *nextFreeTable;
132 unsigned int arrayBytesNeeded, tableBytesNeeded, dimProduct, slop;
137 tableBytesNeeded = 0;
139 for(i=0; i<nDim-1; i++)
141 dimProduct *= Dims[i];
142 tableBytesNeeded += dimProduct*
sizeof(
char *);
146 slop = (tableBytesNeeded % sizeofelt);
148 slop = sizeofelt - slop;
151 dimProduct *= Dims[i];
152 arrayBytesNeeded = dimProduct*sizeofelt;
155 ptr = malloc( tableBytesNeeded + slop + arrayBytesNeeded );
156 if (ptr == NULL)
return (
char *) NULL;
160 arrayBase = ptr + slop + tableBytesNeeded;
163 nextFreeTable = tableBase;
164 nextFreeArray = arrayBase;
168 &nextFreeArray
, sizeofelt
);
173 if (nextFreeTable != arrayBase - slop ||
174 nextFreeArray != ptr + tableBytesNeeded + slop + arrayBytesNeeded)
176 fprintf( stderr,
"Error in mallocNd! Memory overwrite." );
179 return(
char *) tableBase;
188 char ***nextFreeTable, **nextFreeArray;
192 char *ReturnValue, **table;
195 ReturnValue = *nextFreeArray;
196 *nextFreeArray += sizeofelt * Dims[0];
201 table = *nextFreeTable;
202 *nextFreeTable += Dims[0];
203 for(i=0; i<Dims[0]; i++)
206 nextFreeArray
, sizeofelt
);
208 ReturnValue = (
char *) table;
char * BuildIndirectionTable(unsigned nDim, Dims, char ***nextFreeTable, char **nextFreeArray, unsigned sizeofelt)
char * malloc_2d(int n, int m, int sizeofelt)
char * mallocNd(unsigned int nDim, Dims, unsigned int sizeofelt)