Process is terminated due to StackOverflowException. 是什么缘故
这是一个折半查找的代码:
#define N 10000000
int main()
{
int a[N],i;
for (i=0;i<N;i++)
a[i]=i;
int x=0,end=0,start=0,middle=0;
scanf ("%d",&x);
start=0;
end=N-1;
while (start<=end)
{
middle=(start+end)/2;
if ( x> a[middle] )
{
start = middle + 1;
} else if (x < a[middle]) {
end = middle - 1;
} else {
printf ("The number %d has been found!\n",x);
exit (0);
}
}
}
如果把 #define N 1000000 改为 #define N 100 就没事了,而改为 #define N 122976 运行没有问题而输入有问题,N 大于122976 运行后立即出现问题。难道C语言限制数组容量吗?