【发布时间】: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