C++ 在Eigen中设置列中的返回值
当我运行此方法时,当我访问返回值C++ 在Eigen中设置列中的返回值,c++,eigen,C++,Eigen,当我运行此方法时,当我访问返回值M_k或程序完成时,程序崩溃。当我只使用rowmajor存储时,类似的代码也能工作。如果我注释掉行*M_k=temps它也可以工作,但是我当然想要返回值 typedef Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor> Matrix_CM_t; typedef Eigen::SparseMatrix<double, Eigen::RowMajor>
M_k
或程序完成时,程序崩溃。当我只使用rowmajor存储时,类似的代码也能工作。如果我注释掉行*M_k=temps代码>它也可以工作,但是我当然想要返回值
typedef Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor> Matrix_CM_t;
typedef Eigen::SparseMatrix<double, Eigen::RowMajor> ESMatrix_t;
typedef Eigen::SparseMatrix<double, Eigen::ColMajor> ESMatrix_CM_t;
typedef Eigen::Matrix<double, Eigen::Dynamic, 1> Vector_t;
/* M_k = M(I(1:k),:) */
void fill_matrix_from_first_rows_from_list(ESMatrix_t *M, Vector_t *I, int64_t k, ESMatrix_CM_t *M_k) {
int64_t i;
Matrix_CM_t temp = Matrix_CM_t(k, M->cols());
ESMatrix_CM_t temps;
#pragma omp parallel shared(M,M_k,I,k) private(i,row_vec)
{
#pragma omp for
for (i = 0; i<k; i++) {
temp.row(i) = M->row(I->coeffRef(i, 1));
}
}
temps = temp.sparseView();
temps.makeCompressed();
*M_k = temps;
}
类型ESMatrix\u CM\t
是否具有副本构造函数。它可能会失败,因为该类型包含无法进行简单复制的指针。当函数返回时,类型中的指针超出范围,内存变为无效。能否提供调用该方法并重现问题的代码?另外,复制问题是否需要并行化?我建议使用内存检查器运行程序,以查看内存损坏的位置。@CodeGorilla AllEigen::SparseMatrix
具有副本构造函数b=&Vector_t()代码>和CS=&ESMatrix_CM_t()代码>对我来说似乎很奇怪。你取了一个临时工的地址。为什么要将b
和CS
声明为指针?
int main(int argc, char* argv[])
{
Matrix_t temp;
Vector_t *a, tempv;
ESMatrix_t *AS, temp3;
ESMatrix_CM_t *CS;
tempv = Vector_t(3);
tempv << 2,0,1;
a = &tempv;
b = &Vector_t();
temp = Matrix_t(3, 3);
temp << 0, 0, 2, 3, 0, 0, 0, 1, 0;
temp3 = ESMatrix_t(3, 3);
temp3 = temp.sparseView();
temp3.makeCompressed();
AS = &temp3;
CS = &ESMatrix_CM_t();
cout << "A before " << endl << *AS << endl;
fill_matrix_from_first_rows_from_list(AS, b);
cout << "Here is the matrix A:\n" << *CS << endl;
}