Hi all,
I have been trying to figure out the problem with my allocation and use of a multidimensional dynamically allocated array in C. I'd really appreciate any help.
I've tried two approaches. The first:
cdr = (double ***) malloc(NUM_REGIONS * sizeof(double **));
for(i=0; i<NUM_REGIONS; i++){
cdr[i] = (double **) malloc(numRatings * sizeof(double *));
for(j=0; j<numRatings; j++){
cdr[i][j] = (double *) malloc(remQuarters * sizeof(double));
}
}
And the second:
tempPtr1 = (double *) malloc(NUM_REGIONS * numRatings * remQuarters * sizeof(double) );
tempPtr2 = (double **) malloc (NUM_REGIONS * numRatings * sizeof(double *));
cdr = (double ***) malloc(NUM_REGIONS * sizeof(double **));
for(i=0; i< NUM_REGIONS; i++){
cdr[i] = tempPtr2 + i;
for(j=0; j < numRatings; j++) cdr[i][j] = tempPtr1 + i * NUM_REGIONS + j;
}
Neither is working. In both cases, each cdr[i] ends up pointing to the same place. The first time I step into the 'i' loop, all cdr[i] (i.e. cdr[0], cdr[1], cdr[2], etc.) get set to the same value. Subsequent loops then don't change any of them.
I suspect there's something going on with operator precedence or I'm dereferencing wrong, but I haven't been able to figure it out.
Thanks.
UPDATE
I put together the following simplified code, which seems to work fine. But while the output is completely as expected, I'm still getting the same weird behaviour as I step through it in the debugger. I'm starting to think the fundamental problem with my code might be elsewhere, and I've just been diverted by issues with the debugger (or probably just with my misunderstanding of the output). Is there a known reason why a watch on 'cdr[0]', 'cdr[1]', etc. in Visual Studio wouldn't show what I'm expecting it to show?
#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"
#define NUM_REGIONS 50
void printArray(double *inVec, int len){
int i;
for(i=0; i<len; i++) printf("%f\t",inVec[i]);
printf("\n");
}
int main(array<System::String ^> ^args){
int numRatings = 25, remQuarters = 100, i, j, k;
double ***cdr;
char dummy;
cdr = (double ***) malloc(NUM_REGIONS * sizeof(double **));
for(i=0; i<NUM_REGIONS; i++){
cdr[i] = (double **) malloc(numRatings * sizeof(double *));
for(j=0; j<numRatings; j++){
cdr[i][j] = (double *) malloc(remQuarters * sizeof(double));
}
}
for(i=0; i<NUM_REGIONS; i++){
for(j=0; j<numRatings; j++){
for(k=0; k<remQuarters; k++){
cdr[i][j][k] = 100*i + 10*j +k;
}
}
}
for(i=0; i<5; i++) printf("%f\t",cdr[1][1][i]);
printf("\n");
for(i=0; i<5; i++) printf("%f\t",cdr[3][1][i]);
printf("\n");
for(i=0; i<5; i++) printf("%f\t",cdr[1][3][i]);
printf("\n");
for(i=0; i<5; i++) printf("%f\t",cdr[i][i][i]);
printf("\n");
printArray(cdr[1][1], 5);
printArray(cdr[3][3], 5);
scanf("%c", &dummy);
return 0;
}
Thanks again for all the feedback.