变量周围的堆栈'Yarray'已损坏

Stack around the variable 'Yarray' was corrupted

本文关键字:Yarray 已损坏 周围 变量 堆栈      更新时间:2023-10-16

当我声明一个数组来存储每个坐标的 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
}
// values could be null, you should check that
// if instead of int* values, you took std::vector<int>& values
// You know besides the values, the quantity of them
void EquationElement::getPolynomial(const int* values)
{
    //Takes in coefficients to calculate Y values for a polynomial//
    static const int size = 40; // No reason for size to be double
    static const int step = 1; // No reason for step to be double
    int Yarray[2*size+1]{}; // 40 will not do {} makes them initialized to zero with C++11 onwards
    int third = values[0];
    int second = values[1]; // avoid pointer arithmetic
    int first = values[2]; // [] will work with std::vector and is clearer
    int constant = values[3]; // Values should point at least to 4 numbers; responsability goes to caller
    for (int i = 0; i < 2*size + 1; ++i) {
        double x = (i - (size)) * step; // x goes from -40 to 40
        double Y = (third *(x*x*x)) + (second *(x*x)) + (first * (x)) + constant;
        // Seems unnatural that x^1 is values and x^3 is values+2, being constant at values+3
        double Yvalue= Y / step; // as x and Yvalue will not be used outside the loop, no need to declare them there
        Yarray[i] = int(round(Yvalue));  //<-MAIN ISSUE HERE?//
        // Yep, big issue, i goes from 0 to size*2; you need size+size+1 elements
        cout << Yarray[i] << endl;  
    }
}

而不是

void EquationElement::getPolynomial(const int* values)

您也可以声明

void EquationElement::getPolynomial(const int (&values)[4])

这意味着现在你需要用指向 4 个元素的指针来调用它;不多也不少。

另外,std::vector

void EquationElement::getPolynomial(const std::vector<int>& values)
{
    //Takes in coefficients to calculate Y values for a polynomial//
    static const int size = 40; // No reason for size to be double
    static const int step = 1; // No reason for step to be double
    std::vector<int> Yarray;
    Yarray.reserve(2*size+1); // This is just optimization. Yarran *Can* grow above this limit.
    int third = values[0];
    int second = values[1]; // avoid pointer arithmetic
    int first = values[2]; // [] will work with std::vector and is clearer
    int constant = values[3]; // Values should point at least to 4 numbers; responsability goes to caller
    for (int i = 0; i < 2*size + 1; ++i) {
        double x = (i - (size)) * step; // x goes from -40 to 40
        double Y = (third *(x*x*x)) + (second *(x*x)) + (first * (x)) + constant;
        // Seems unnatural that x^1 is values and x^3 is values+2, being constant at values+3
        double Yvalue= Y / step; // as x and Yvalue will not be used outside the loop, no need to declare them there
        Yarray.push_back(int(round(Yvalue)));
        cout << Yarray.back() << endl;  
    }
}