【算法】main函数的堆栈溢出

main函数的堆栈的大小默认为1mb

如果把数组int x[1000][1000]定义在main函数里

则int为4byte,8bit为1byte,1024byte为1kb,1024kb为1mb

4*1000*1000/1024/1024=3.814697265625mb大于1mb,

所以定义在main函数中会出现堆栈溢出的异常

#include<bits/stdc++.h>
using namespace std;
int main(){
    int x[1000][1000];
    return 0; 
} 

 结果

--------------------------------
Process exited after 3.482 seconds with return value 3221225725
请按任意键继续. . .

解决办法是将数组定义在main函数外static静态分配储存空间

建议在竞赛中尽量将数组定义在main函数外

#include<bits/stdc++.h>
using namespace std;
int x[1000][1000];
int main(){
    return 0; 
} 

猜你喜欢

转载自www.cnblogs.com/LPworld/p/11904819.html
今日推荐