block1可以正常执行但是block2会出现内存错误,想了很久没有明白。求解答
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void** malloc2d(int rows, int cols, int size)
{
printf("%d\t%d\t%d\n",rows,cols,size);
int j;
int rowSize = cols * size;
int indexSize = rows * sizeof(void *);
void **a = (void **) malloc(indexSize + rows* rowSize);
char *dataStart = (char *) a + indexSize;
for(j = 0; j < rows; j++){
printf("%d\n",j);
a[j] = dataStart + j * rowSize;
}
return a;
}
int main(){
int i,rows,cols,row,col;
rows = cols = 40400;
row = 3;col = 2;
int** block1 = (int**)malloc2d(row,col,sizeof(int));
printf("end of block1,begin of block2\n");
int** block2 = (int**)malloc2d(rows,cols,sizeof(int));
free(block1);
free(block2);
return 0;
}