[Rcpp-devel] Eigen-like pointer mappings in Rcpp?
Sparapani, Rodney
rsparapa at mcw.edu
Tue Feb 17 20:53:42 CET 2015
Hi Gang:
I am working with some C++ code that uses pointers. I know I could
change the code. But, I was wondering if there is an easy way to
map the double pointer into the R data via Rcpp. I have been using
RcppEigen to do that. For example...
RcppExport SEXP myfunc(SEXP _n, SEXP _x) {
const int n=Rcpp::as<int>(_n);
double *x=(double *)malloc(n*sizeof(double));
Eigen::Map<Eigen::VectorXd> map_x(x, n);
map_x=Rcpp::as< Eigen::Map<Eigen::VectorXd> >(_x);
double a=fit(x, n);
...
It seems to me there should be an easier way like ...
RcppExport SEXP myfunc(SEXP _n, SEXP _x) {
const int n=Rcpp::as<int>(_n);
double *x=Rcpp::as<double*>(_x); // or a const-correct version
double a=fit(x, n);
...
Thanks,
Rodney
More information about the Rcpp-devel
mailing list