首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

请大神看一下,为啥会出现stack overflow

2013-04-21 
请大神看一下,为什么会出现stack overflow#includeiostream#includeiomanip#includecmathusing name

请大神看一下,为什么会出现stack overflow



#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;

const double oo=1e100;

double distance(double &x1,double &y1,double &x2,double &y2)
{
double dis=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
return sqrt(dis);
}

int main()
{
double x[100000]={0};
double y[100000]={0};

int n;
while(cin>>n)
{
double minDistance=oo;

if(n==0)
break;

for(int i=0;i<n;i++)
{
cin>>x[i]>>y[i];
}

for(int j=0;j<n;j++)
{
for(int h=j+1;h<n;h++)
{
double pointDistance=distance(x[j],y[j],x[h],y[h]);
if(pointDistance<minDistance)
minDistance=pointDistance;
}
}

double ans=minDistance/2.0;
cout<<setiosflags(ios::fixed)<<setprecision(2)<<ans<<endl;
}
return 0;
}
[解决办法]
double x[100000]={0};
double y[100000]={0};
----------------------
改成动态数组试试
double *x = new double[100000];
double *y = new double[100000];
...
delete []x;
delete []y;

[解决办法]
2个数组分配的太大了?
这种场合,用动态分配的会比较好
[解决办法]
double x[100000]={0};
double y[100000]={0};

亲,你资源真多。。new吧。
[解决办法]
1. 类似double x[100000]={0};这种静态内存分配,消耗的都是stack(栈)上的内存。
2. 一般编译器给出的缺省stack的大小是1M(但可以通过编译参数调整的)
3. double x[100000]={0}; double y[100000]={0};相当于200K个double,一个double是8bytes,那么这两个数组所占的空间就是:200K * 8Bytes = 1,600KBytes = 1.6M > 1M,所以stack就overflow了

热点排行