![](/img/trans.png)
[英]Run-Time Check Failure #2 - Stack around the variable 'IDNumber' was corrupted
[英]Run-Time Check Failure #2 - Stack around the variable 'k' was corrupted
练习:找出1到1000之间的完美数字(完美数字:例如6,因为6 = 1 + 2 + 3),这是我的代码。
#include<iostream>
using namespace std;
int main()
{
int k[11];
int i, a, n, s;
for (a = 2; a <= 1000; a++)
{
n = 0;
s = a;
for (i = 1; i < a; i++)
if (a%i == 0)
{
n++;
s = s - i;
k[n] = i;
}
if (s == 0)
{
cout << a << " is a perfect number" << endl;
cout << "its factors are:";
for (i = 1; i <= n; i++)cout << k[i] << " ";
cout << endl;
}
}
return 0;
}
但它显示了运行时检查失败#2-当我将int k[11]
更改为int k[32]
时,围绕变量'k'的堆栈已损坏。
数组元素至少为32,为什么?
数组索引从0开始。您将n增加得太早。 显然,数组大小太小10。 什么公式给出该大小?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.