问题描述
DEV-C报错 Process exited after 4.03 seconds with return value 3221225725
原因
数组定义的容量太大 - 五十万起步的样子
而且每次循环都会再定义一次,导致缓存区溢出
解决办法:
思路来源:
https://blog.csdn.net/qq_48508278/article/details/120358623
办法:数组定义在主函数外面 作为全局变量
#include<bits/stdc++.h>
#define num 10000
#define cmp <
using namespace std;
long a[1500000]; // 一百五十万
int main()
{
srand(time(NULL));
clock_t start, end;
double Total_time;
long length;
long l = 600000;
long b[10];
for(int i = 0;i<10;i++)
{
b[i] = l;
l = l+100000;
}
for(l=0;l<10;l++)
{
start = clock();
int k;
length = b[l];
for(k = 0;k<length;k++)
{
a[k] = rand()%num;
}
int i, j, temp;
cout << "length = " << length << endl;
cout << "开始直接插入排序(for) ";
for(i = 1; i < length; i++)
{
temp = a[i];
for(j = i-1; j >= 0; j--)
{
if(temp cmp a[j])
a[j + 1] = a[j];
else
break;
}
a[j + 1] = temp;
}
cout << "排序完成! ";
end = clock();
Total_time = (double)(end - start) / CLOCKS_PER_SEC;
cout << "用时 = " << Total_time << endl <<endl;
start = end;
}
system("pause");
return 0;
}