I use C in Unix OS. When I use "malloc" to allocate a space for a 2D array which is defined as **xx. It can't be allocated the size as I want. For example, if I want to give it 31*31 size, it is told "bus error". If I only give it 2*2, it passed. And if I allocate it in the beginning of the function, it passed. While if I allocate it where it should be, it appeared the error above.
I allocate it as this:
int **sub;
sub=(int**)malloc(31*sizeof(int *));
for(row=0;row<31;row++)
sub[row]=(int *)malloc(31*sizeof(int));
Could anybody tell me what's wrong in my code?
Any help would be greatly appreciated.
I allocate it as this:
int **sub;
sub=(int**)malloc(31*sizeof(int *));
for(row=0;row<31;row++)
sub[row]=(int *)malloc(31*sizeof(int));
Could anybody tell me what's wrong in my code?
Any help would be greatly appreciated.