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;
}
是因為2的數據太多了
void **a = (void **) malloc(indexSize + rows* rowSize);,分配失敗,返回空值