奇怪的内存大小分配问题
代码如下,
int main(int argc, _TCHAR* argv[])
{
float *u_h = 0;
float *f_h = 0;
int ArraySizeX = 1024;
int ArraySizeY = 1024;
int size = ArraySizeX * ArraySizeY * sizeof(int);
u_h = (float*)malloc(size);
f_h = (float*)malloc(size);
float *tmp_u_h, *tmp_f_h;
tmp_u_h = u_h;
tmp_f_h = f_h;
if (u_h == NULL)
{
printf("allocation error");
exit(1);
}
else
printf("+++++%p+++++\n,", u_h);
// if (u_h!=0)
// free(u_h);
if (tmp_u_h == 0)
printf("NULLNULLNULLNULL");
float h = 1.0;
int i = 0;
for (i=0; i<1024*1024; i++)
{
*u_h = 3.0;
*f_h = 4.0;
u_h++;
f_h++;
}
printf("+++++%p+++++\n,", tmp_u_h);
if (tmp_u_h!=0)
free(tmp_u_h);
free(tmp_f_h);
printf("**********Execuation is done*********!!!\n");
getchar();
return 0;
}
对于循环
for (i=0; i<1024*1024; i++)
{
*u_h = 3.0;
*f_h = 4.0;
u_h++;
f_h++;
}
如果i<1024或者一个比较小的数字时候,整个代码运行错误,可是如果变成1024×1024就回有内存错误,不能执行free操作。希望高手能够指教,不知道是不是动态分配内存大小有限制呢。我使用的是Linux 64bit的机器。内存8G。