原因
程序调用的栈过大时,超过系统默认分配的大小时,即出现栈溢出。
案例与解决方案
#include<iostream> using namespace std; void PrintN(int N) { if (N > 0) { PrintN(N - 1); } cout << N << endl; } void main() { int N=1000000; PrintN(N); system("pause"); }
PrintN时通过递归方法输出递减输出N个数的函数,虽然用递归的方法写,比较简明清楚,但是空间复杂度会较大。
容易发生栈溢出stack overflow。
流程如下:
右击项目,选择属性
连接器->系统->堆栈保留大小
可设置为一个大数字,注意其单位为k