Reputation: 537
I try to calloc 2d array and initial value of edge cell with 255, it work correctly but when I try to set dimension of array over than 12000*12000, VS2010 show Access violation writing location 0x00000000
my function of calloc 2d array
int **calloc_2d_int(int Rows, int Cols) {
int *data = (int *)calloc(Rows*Cols,sizeof(int));
int **array= (int **)calloc(Rows,sizeof(int*));
for (int i=0; i<Rows; i++)
array[i] = &(data[Cols*i]);
return array;
}
main function
int main(int argc, char* argv[]){
int i,j;
/*convert string to int*/
int row = atoi(argv[1]);
int column = atoi(argv[2]);
int T = atoi(argv[3]);
int** s[2];
s[0] = calloc_2d_int(row,column);
s[1] = calloc_2d_int(row,column);
for(i=0 ; i<row ; i++){
s[0][i][0] = 255; //error at this line
s[1][i][0] = 255;
s[0][i][column-1] = 255;
s[1][i][column-1] = 255;
}
for(i=0 ; i<column ; i++){
s[0][0][i] = 255;
s[1][0][i] = 255;
s[0][row-1][i] = 255;
s[1][row-1][i] = 255;
}
}
Thank you
Upvotes: 1
Views: 783
Reputation: 5917
Access violation writing location 0x00000000
This means you are trying to write on a NULL
pointer. So calloc
returned NULL
.
As you can read on the calloc manpage, it returns NULL
on error, which can be a too big memory request.
Depending of your system, 13000 * 13000 (169 000 000) might be too big.
Always check malloc
, calloc
and realloc
returns for an error.
Upvotes: 4