【问题标题】:Segmentation fault (core dumped)-cannot fix error分段错误(核心转储)-无法修复错误
【发布时间】:2016-03-13 10:50:25
【问题描述】:

以下代码有问题。我正在使用 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;

}

感谢您的帮助。这个错误让我发疯。

【问题讨论】:

  • 在哪里发生崩溃?请在调试器中运行 debug-build 以捕捉正在发生的崩溃,并使用调试器在代码中定位崩溃的位置。
  • 你说你尝试了几件事。你尝试了什么?您是否尝试过使用gdb?
  • 最明显的错误是你没有初始化temp1(不要为其分配空间指向),而是为*temp1赋值。那是疏忽吗?还是需要学习 C/C++ 中指针的基础知识?

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


【解决方案1】:

你声明了几个指针:

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

然后您立即继续取消引用未初始化的指针:

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

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

这是你的问题。

附:您应该花一些时间来学习如何使用调试器。使用调试器应该很容易解决这个问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-23
    • 1970-01-01
    • 2014-08-04
    • 2012-11-19
    • 1970-01-01
    • 1970-01-01
    • 2019-03-14
    • 2015-06-25
    相关资源
    最近更新 更多