【发布时间】:2019-09-04 07:45:06
【问题描述】:
当我声明一个数组来存储每个坐标的 Y 值,定义它的值然后使用每个元素值发送到舍入函数时,我得到错误“运行时检查失败 #2 - 堆栈周围变量'Yarray;已损坏。输出大部分是预期的,尽管我想知道为什么会发生这种情况,如果我能减轻它,干杯。
void EquationElement::getPolynomial(int * values)
{
//Takes in coefficients to calculate Y values for a polynomial//
double size = 40;
double step = 1;
int Yarray[40];
int third = *values;
int second = *(values + 1);
int first = *(values + 2);
int constant = *(values + 3);
double x, Yvalue;
for (int i = 0; i < size + size + 1; ++i) {
x = (i - (size));
x = x * step;
double Y = (third *(x*x*x)) + (second *(x*x)) + (first * (x))
Yvalue = Y / step;
Yarray[i] = int(round(Yvalue)); //<-MAIN ISSUE HERE?//
cout << Yarray[i] << endl;
}
}
double EquationElement::round(double number)
{
return number < 0.0 ? ceil(number - 0.5) : floor(number + 0.5);
// if n<0 then ceil(n-0.5) else if >0 floor(n+0.5) ceil to round up floor to round down
}
【问题讨论】:
-
为什么你有
size + size + 1作为循环的上限?这使得i最多 81 个,但Yarray只有 40 个元素。 -
刚刚注意到您的评论。这就是发生错误的原因。
标签: c++ arrays exception stack