请看一下代码:
如果MaxArray1<64可以正常执行,但是只要>=64,就会"非法操作"不能执行下去.#include <stdio.h>#define MaxArray1 63
#define MaxArray2 1024//把数组的值打印出来
void PrintArray(float TestPrintArray[][MaxArray2]);
//把一维数组的值打印出来
void PrintArrayOne(float TestPrintOne[]);
//改变数组的值:+100
void ChangeArray(float TestChangeArray[][MaxArray2]);//void SaveDataToFile(float TestChangeArray[][MaxArray2]);void main()
{
float TestArray[MaxArray1][MaxArray2];
float TestArray1[MaxArray1][MaxArray2];
float TestArray2[MaxArray1][MaxArray2];
float TestArray3[MaxArray1][MaxArray2];
// float TestArrayOne[MaxArray1];
int i,j; //给四个数组赋值
for(i=0;i<MaxArray1;i++)
{
for(j=0;j<MaxArray2;j++)
{
TestArray[i][j] = i+j;
TestArray1[i][j] = i+j;
TestArray2[i][j] = i+j;
TestArray3[i][j] = i+j;
}
}/* for(i=0;i<MaxArray1;i++)
{
TestArrayOne[i] = i;
}
*/
//PrintArrayOne(TestArrayOne);// PrintArray(TestArray);
// printf("\n\n"); //改变四个数组的值
ChangeArray(TestArray);
ChangeArray(TestArray1);
ChangeArray(TestArray2);
ChangeArray(TestArray3); //输出
PrintArray(TestArray); return;
}
void PrintArrayOne(float TestPrintOne[])
{
int i;
for(i=0;i<MaxArray1;i++)
printf(" %f ",TestPrintOne[i]);
}void PrintArray(float TestPrintArray[][MaxArray2])
{
int i,j;
for(i=0;i<MaxArray1;i++)
{
for(j=0;j<MaxArray2;j++)
{
printf(" %f ",TestPrintArray[i][j]);
}
printf("\n");
}
}void ChangeArray(float TestChangeArray[][MaxArray2])
{
int i,j; for(i=0;i<MaxArray1;i++)
{
for(j=0;j<MaxArray2;j++)
{
TestChangeArray[i][j] = TestChangeArray[i][j] + 100;
}
}
}
如果MaxArray1<64可以正常执行,但是只要>=64,就会"非法操作"不能执行下去.#include <stdio.h>#define MaxArray1 63
#define MaxArray2 1024//把数组的值打印出来
void PrintArray(float TestPrintArray[][MaxArray2]);
//把一维数组的值打印出来
void PrintArrayOne(float TestPrintOne[]);
//改变数组的值:+100
void ChangeArray(float TestChangeArray[][MaxArray2]);//void SaveDataToFile(float TestChangeArray[][MaxArray2]);void main()
{
float TestArray[MaxArray1][MaxArray2];
float TestArray1[MaxArray1][MaxArray2];
float TestArray2[MaxArray1][MaxArray2];
float TestArray3[MaxArray1][MaxArray2];
// float TestArrayOne[MaxArray1];
int i,j; //给四个数组赋值
for(i=0;i<MaxArray1;i++)
{
for(j=0;j<MaxArray2;j++)
{
TestArray[i][j] = i+j;
TestArray1[i][j] = i+j;
TestArray2[i][j] = i+j;
TestArray3[i][j] = i+j;
}
}/* for(i=0;i<MaxArray1;i++)
{
TestArrayOne[i] = i;
}
*/
//PrintArrayOne(TestArrayOne);// PrintArray(TestArray);
// printf("\n\n"); //改变四个数组的值
ChangeArray(TestArray);
ChangeArray(TestArray1);
ChangeArray(TestArray2);
ChangeArray(TestArray3); //输出
PrintArray(TestArray); return;
}
void PrintArrayOne(float TestPrintOne[])
{
int i;
for(i=0;i<MaxArray1;i++)
printf(" %f ",TestPrintOne[i]);
}void PrintArray(float TestPrintArray[][MaxArray2])
{
int i,j;
for(i=0;i<MaxArray1;i++)
{
for(j=0;j<MaxArray2;j++)
{
printf(" %f ",TestPrintArray[i][j]);
}
printf("\n");
}
}void ChangeArray(float TestChangeArray[][MaxArray2])
{
int i,j; for(i=0;i<MaxArray1;i++)
{
for(j=0;j<MaxArray2;j++)
{
TestChangeArray[i][j] = TestChangeArray[i][j] + 100;
}
}
}
解决的办法就是使用动态的方式
这里是一个使用malloc的方式
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>int main(int argc, char *argv[])
{
int **p;
int i,j;
p=malloc(4*sizeof(int *));
for(i=0;i<4;i++)
*(p+i)=malloc(5*sizeof(int));
for(i=0;i<4;i++)
for(j=0;j<5;j++)
scanf("%d",*(p+i)+j);
for(i=0;i<4;i++)
{
for(j=0;j<5;j++)
printf("%d\t",*(*(p+i)+j));
printf("\n");
}
free(p);
system("PAUSE");
return 0;
}
使用new用同样的道理
当然也可以分配的时候计算好了大小然后通过计算来确定数据的位置
float TestArray[MaxArray1][MaxArray2];
方式定义的话用的是程序栈的内存,一般程序会有1M左右的栈内存,
这是用来让程序调函数,和写成局部的变量.\而用new的会在堆上生成内存!
则要占用内存1024*64*2B=128M加上1024*2B=2M.
共130M,
再加上你所运行的环境上被OS所占用的空间(系统区),以及用户区上其它应用软件所占的空间,可能已经使MS WINDOWS的虚拟机制出现了问题,从而无法实现,所以你的要求无法满足.到现在我想不可能还有什么1M的限制的,一个32位的机器好像不应该有这样的限制.