变量“Yarray”周围的堆栈已损坏

Posted

技术标签:

【中文标题】变量“Yarray”周围的堆栈已损坏【英文标题】:Stack around the variable 'Yarray' was corrupted 【发布时间】: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 个元素。 刚刚注意到您的评论。这就是发生错误的原因。 【参考方案1】:
// 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;  
    

【讨论】:

以上是关于变量“Yarray”周围的堆栈已损坏的主要内容,如果未能解决你的问题,请参考以下文章

运行时检查失败 #2 - 变量“结果”周围的堆栈已损坏

变量周围的堆栈已损坏(将 long long 转换为字节数组)

VS2010 和 OpenCV:变量周围的堆栈已损坏

c++ 运行时检查失败 #2 - 变量“ToSend22”周围的堆栈已损坏

运行时检查失败 #2 - 变量周围的堆栈已损坏

运行时检查失败 #2 - 变量“l1”周围的堆栈已损坏