I am making several calls to a function that initializes 2-d arrays of doubles. The first three calls are successful, but the fourth call fails. Code is:
double **scmass;
double **scdv;
double **dvmatches;
double **zpp;
…
scmass = init_d2darr(numrow,numcol);
scdv = init_d2darr(numrow,numcol);
dvmatches = init_d2darr(numrow,numcol);
zpp = init_d2darr(numrow,numcol);
…
double ** init_d2darr(int isize, int jsize) {
int m,n;
printf("in array initialize 1\n");
printf("isize: %i\njsize: %i\n",isize,jsize);
/* how many rows */
double **arr = (double **)malloc(sizeof(double *) * isize);
printf("after row initialization\n");
if (arr == NULL) {
printf("malloc failed for arr in init_d2darr.\n");
return NULL;
}
else {
/* now how many cols */
for (m=0; m<=isize; m++) {
arr[m] = (double *)malloc(sizeof(double) * jsize);
if (arr[m] == NULL) {
printf("malloc failed for arr[%d] in init_d2darr.\n",m);
return NULL;
}
else {
/* initialize all to zero */
for (n=0; n<jsize; n++) {
arr[m][n] = 0;
}
}
}
}
printf("in array initialize 2\n");
return arr;
Output looks like:
in array initialize 1
isize: 20
jsize: 21
after row initialization
in array initialize 2
in array initialize 1
isize: 20
jsize: 21
after row initialization
in array initialize 2
in array initialize 1
isize: 20
jsize: 21
The program actually runs all the way through when in debug mode, however, when I make a release executable and attempt to run it, the program crashes and I get the error “A problem caused the program to stop working correctly. Please close the program.” I’m on windows vista with Visual C++ 2008 Express Edition. Could anyone give me any advice? Thanks!
Jade
Edit: the declaration of the function (in a header file) is
double ** init_d2darr(int isize, int jsize);
In your outer for-loop:
this should be: