VS 报错:Run-Time Check Failure #2 - Stack around the variable ‘a‘ was corrupted
##我是在编写以下代码遇到的情况,记录一下以便下次翻阅:
2020.10.12
计算16以内阶乘的头文件:
#include
using namespace std;
int Factorial(int num) {
int a[16];
a[0] = 1;
for (int i = 1; i < 17; i++) {
a[i] = i * a[i - 1];
}
return a[num];
}
`
main文件:
#include
#include “ForLoop.h”
#include “express.h”
#include “6_Factorial.h”
int main()
{
//forloop();
std::cout << “Hello World!\n”;
//express();
// forloop();
cout << "10! = " << Factorial(10) << endl;
return 0;
}
###运行之后显示如图:
意思是在a堆栈内存有被破坏,然后就是我申请的a数组只有十六个数据,然后我实际是保存了17个数据,将数组扩大为17之后显示:
正常显示了