[英]Run-Time Check Failure #2 - Stack around the variable 'k' was corrupted
an exercise:find out the perfect number from 1 to 1000 (perfect number:such as 6,because 6=1+2+3) this is my code. 练习:找出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;
}
but it shows Run-Time Check Failure #2 - Stack around the variable 'k' was corrupted when I change int k[11]
to int k[32]
it is correct. 但它显示了运行时检查失败#2-当我将
int k[11]
更改为int k[32]
时,围绕变量'k'的堆栈已损坏。
the array element is at least 32 ,so why? 数组元素至少为32,为什么?
Array indices start at 0. You are incrementing n too early. 数组索引从0开始。您将n增加得太早。 And obviously, 10 is too small as the array size.
显然,数组大小太小10。 What formula gives this size?
什么公式给出该大小?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.