to laomai
那么使用malloc申请内存时是否有大小限制,我用这个方法写了个程序,运行的时候申请一块较小的内存时,一切正常,但当我申请一块较大的内存是就出错了(申请到的内存地址等于NULL)。然后我用单步调试去执行,这个错误又不再出现,搞得我一头雾水,能否告知原因呢,多谢了!
下面是我的程序的代码:
#include <stdio.h>
int *** createarray(int ***p, int rows, int cols);
int *** freearray(int ***p, int rows, int cols);
int *** createarray(int ***p, int rows, int cols)
{ int i, j;
p=(int ***)malloc(sizeof(int **) * rows);
for( i=0;i<rows;i++)
{ p[i] = (int **)malloc(sizeof(int) * cols );
if (p[i]==NULL)
{ printf("Create array error!!!\n");
exit(1);
}
for ( j=0;j<cols;j++)
{ p[i][j] = (int*)malloc(sizeof(int) * 3 );
if (p[i][j]==NULL)
{ printf("Create array error!!!\n");
exit(1);
}
}
}
return p;
}
int *** freearray(int ***p, int rows, int cols)
{ int i,j;
for( i=0;i<rows;i++)
{ for( j=0;j<cols;j++)
{ free(p[i][j]) ;
}
free(p[i]);
}
free(p);
return NULL;
}
void main()
{ char *filename_in;
char *filename_out;
char ch;
int rows, cols;
int max_col;
FILE *fp_in, *fp_out;
int i, j, k, l, n, nn, nnn;
int ***ppixel;