Eigen
라이브러리를 사용하여 다음 코드로 이상한 동작이 발생했습니다.메서드에서 전달한 후 포인터 반환 값이 변경됨
포인터를 DoubleListCg->doubles
에 할당하면 DoubleListCg->doubles[i]
에 대한 값이 잘못되었습니다. 하지만 요소를 하나씩 할당하면 위의 포인터 배열에 대한 값이 정확합니다. 왜?
#define DllExport __declspec(dllexport)
#include "stdafx.h"
#define EIGEN_USE_MKL_ALL
#include <iostream>
#include "Eigen/Dense"
#include "Eigen/src/Core/util/MKL_support.h"
#include <Eigen/Sparse>
#include <Eigen/Eigenvalues>
#include <vector>
#include <Eigen/PardisoSupport>
using namespace std;
using namespace Eigen;
extern "C" struct DllExport DoubleListCg
{
int num_points;
double* doubles;
};
// working
void DoubleList(VectorXd vector, DoubleListCg *dListPtr)
{
dListPtr->num_points = vector.size();
dListPtr->doubles= (double*) malloc (dListPtr->num_points*sizeof(double));
for (int i = 0; i < vector.size(); i++)
{
dListPtr->doubles[i]=vector[i];
}
}
//not working
void DoubleList2(VectorXd vector, DoubleListCg *dList)
{
dList->num_points = vector.size();
dList->doubles =vector.data();
}
void VectorConvert()
{
VectorXd vector(3);
vector<<1,2,3;
DoubleListCg dList;
DoubleList(vector, &dList); //this works; the dList gets the right values at first element
DoubleListCg dList2;
DoubleList2(vector, &dList2) //this doesn't work; the dList2 gets the wrong values at first element
}
그것은 내가 끊고 정말 초등학교 뭔가해야합니다
이 아래에있는 내 코드입니다.
관련 질문 : 어쨌든 dList->doubles
은 vector.data()
에 있습니까? 나는 모든 것을 반복해서 복사하고 싶지 않다.
왜이 질문을 downvoted와 (-재현하지 않는 이유) 폐쇄 선정됐다? – Graviton