先是定义了一个给二维数组动态分配内存的函数:
int **malloc2(int row, int col, int size)
{
int **arr;
arr = (int **) malloc(sizeof(int*) * row + size * row * col);
if (arr != NULL)
{
int *head,count;
head = (int*) arr + sizeof(int *) * row;
memset(arr, 0, sizeof(int *) * row + size * row * col);
//while (row--)
// arr[row] = head + size * row * col;
for(count = 0; count < row ; count++)
{
arr[count] = head + size*count*col;
}
}
return arr;
}
调用:moban=(int**)malloc2(1024, 1024, sizeof(int));
在运行是出现的错误:
NON-FATAL RUN-TIME ERROR: Out-of-bounds pointer arithmetic: 12289 bytes (3073 elements) past end of array.
自己弄了两天了也没解决问题,希望各位高手帮帮我啊,感激涕零! |