【问题标题】:Run-Time Check Failure #2 - Stack around bMatix was corrupted运行时检查失败 #2 - bMatix 周围的堆栈已损坏
【发布时间】:2012-07-10 23:40:37
【问题描述】:

调用树: 填充矩阵(系数,bMatrix,行,列); solveMatrix(coeff, bMatrix, cols+1);

void solveMatrix(float aMatrix[][DEFCOLS+1],float bMatrix[DEFCOLS+1], size_t cols){
std::cout << "\nInside solveMatrix...: " << endl;
size_t N2 = cols;

std::cout << "\N2 is...: " << N2 << endl;
for(size_t p=0; p<N2; p++){
    //std::cout << "\nInside 1st for loop...: " << endl;
    // find pivot row and swap
    int max = p;
    for(size_t i=p+1; i<N2; i++){
        //std::cout << "\nInside 2nd for loop...: " << endl;
        if ( abs(aMatrix[i][p]) > abs(aMatrix[max][p]) ){
            max = i;
        }
    }

    //std::cout << "\nJust b4 all the swapping...: " << endl;

    float temp[] = { *aMatrix[p] };
    *aMatrix[p] = *aMatrix[max];

    *aMatrix[max] = *temp;

    float t = bMatrix[p];
    bMatrix[p] = bMatrix[max];


    bMatrix[max] = t;
    //std::cout << "\nDone all the swapping...: " << endl;

    if ( abs(aMatrix[p][p]) <= MINISCULE) {
        std::cout << "***** Error matrix value too small. Matrix is singular" << endl;
        return;
    }
    // Pivot /in A and b
    for(size_t i=p+1; i<N2; i++){
        float alpha = aMatrix[i][p] / aMatrix[p][p];

        bMatrix[i] = alpha * bMatrix[p];

        for(size_t j=p; j<N2; j++){
            aMatrix[i][j] -= alpha * aMatrix[p][j];
        }

    }

    std::cout << "\nAbout to do the back subst..: " << endl;
    // back subst.
     std::vector<float> outMatrix(N2, 0.0);
     int i =0;

    for(i=N2-1; i>=0; i--){
        float sum = 0.0;
        int j=0;
        for(j=i+1; j<N2; j++){
            sum += aMatrix[i][j] * outMatrix[j];
        }       
        if (aMatrix[i][i] > 0){
            outMatrix[i] =  ( bMatrix[i] - sum ) / aMatrix[i][i];
        }else {
            outMatrix[i] = 0.0;
        }
    }

    int g = 0;
    std::cout << "\nSolution: " << endl;
    for (;g < N2-1; g++) {
        cout << outMatrix[g] << " ";
    }
    cout << endl;
    return;
}  

} // 结束solveMatrix()

这是具有部分旋转的高斯消除的实现。应用程序 运行并给出结果 - 不正确的结果,但在它返回后我得到下面的错误。

只要它返回到调用的顶部,我就会得到: 运行时检查失败 #2 - bMatrix 周围的堆栈已损坏!!!

我一直在看这个,不明白为什么会这样。 请帮忙。谢谢。

【问题讨论】:

    标签: c++ gaussian


    【解决方案1】:

    出现问题是因为您传递给 solveMatrix() 的实际矩阵大小(即 cols+1)大于实际数组的大小(coeff 或 bMatrix)。

    最简单的调试方法如下:

    1. 完全清除函数体。确保您不会再收到错误消息。
    2. 恢复第一个 for() 循环。看看这是否会导致出现错误。
    3. ...
    4. 重复此操作,直到找到导致错误的函数的确切部分。然后仔细查看并确保您没有超出数组限制(即,如果 bMatrix 是 float[16],则您没有编写 bMatrix[16]、[17] 或更多。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-09-21
      • 2013-12-13
      • 2013-12-10
      • 2015-02-09
      • 2015-05-24
      • 2021-12-31
      • 2014-10-20
      相关资源
      最近更新 更多