分段错误(核心转储) - 无法修复错误

时间:2015-12-08 13:31:09

标签: c++ linux pointers boost memory-segmentation

我遇到以下代码问题。我正在使用Boost进行矩阵乘法。我正在使用Gtesting来测试我的代码。当我测试下面的代码时,我得到以下错误。

Segmentation fault (core dumped)

我知道这与我正在使用的指针有关,但我找不到错误。我尝试了几件事,但没有运气。我的代码如下。我正在运行Ubuntu 14.04。

BLAS::matrix<double>* PolyFilter::getCoef(const std::queue<double> y const std::queue<double> x, const BLAS::vector<double>& w)
{
    int size = y.size();
    queue<double> yList = y;
    BLAS::matrix<double> pos(size,1);
    BLAS::matrix<double>* vand = makeVandermondeMatrix(x);
    BLAS::matrix<double>* weights = makeDiag(w);
    BLAS::matrix<double> *temp1,*temp2,*temp3,*temp4,*temp5;
    BLAS::matrix<double>* temp6 = new BLAS::matrix<double>(size,size);
    std::cout<<size<<endl;


    for( unsigned int i = 0; i < size; i++)
    {
        pos.insert_element(i,0,yList.front());
        yList.pop();
    }

    *temp1 = BLAS::prod(BLAS::trans(*vand), *weights);

    *temp2 = BLAS::prod(*temp1, *vand);


    if( rfalInverse(*temp2, *temp3) )
    {
        *temp4 = BLAS::prod(*temp3, BLAS::trans(*vand));
        *temp5 = BLAS::prod(*temp4,*weights);
        *temp6 = BLAS::prod(*temp5, BLAS::trans(pos));  
    } 



    return temp6;

}

谢谢你的任何帮助。这个错误让我发疯。

1 个答案:

答案 0 :(得分:3)

你宣布了几个指示:

BLAS::matrix<double> *temp1,*temp2,*temp3,*temp4,*temp5;

然后你立即开始取消引用未初始化的指针:

*temp1 = BLAS::prod(BLAS::trans(*vand), *weights);

*temp2 = BLAS::prod(*temp1, *vand);

有你的问题。

P.S。您应该花一些时间学习如何使用调试器。用调试器来解决这个问题应该是微不足道的。