<div dir="ltr"><div><div><div><div><div><div><div>Hello Rodney,<br></div>If all you need is to pass a pointer to the <span style="font-family:monospace,monospace">fit()</span> function (assuming <span style="font-family:monospace,monospace">fit()</span> does not modify <span style="font-family:monospace,monospace">x</span>) and get the result <span style="font-family:monospace,monospace">a</span>, you do not need to make a copy. Simply wrap <span style="font-family:monospace,monospace">_x</span> by a <span style="font-family:monospace,monospace">NumericVector</span> and call the <span style="font-family:monospace,monospace">data()</span> method.<br><br></div><span style="font-family:monospace,monospace">Rcpp::NumericVector x(_x);<br></span></div><span style="font-family:monospace,monospace">double a = fit(x.data(), n);</span><br><br></div>You can even directly use the R API without creating an Rcpp object.<br><br></div><span style="font-family:monospace,monospace">double a = fit(REAL(_x), n);</span><br><br><br><br></div>Best,<br></div>Yixuan<br><div><div><div><br><div><div><div><br><br><br></div></div></div></div></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">2015-02-17 14:53 GMT-05:00 Sparapani, Rodney <span dir="ltr"><<a href="mailto:rsparapa@mcw.edu" target="_blank">rsparapa@mcw.edu</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi Gang:<br>
<br>
I am working with some C++ code that uses pointers.  I know I could<br>
change the code.  But, I was wondering if there is an easy way to<br>
map the double pointer into the R data via Rcpp.  I have been using<br>
RcppEigen to do that.  For example...<br>
<br>
RcppExport SEXP myfunc(SEXP _n, SEXP _x) {<br>
   const int n=Rcpp::as<int>(_n);<br>
<br>
   double *x=(double *)malloc(n*sizeof(double));<br>
<br>
   Eigen::Map<Eigen::VectorXd> map_x(x, n);<br>
<br>
   map_x=Rcpp::as< Eigen::Map<Eigen::VectorXd> >(_x);<br>
<br>
   double a=fit(x, n);<br>
<br>
...<br>
<br>
It seems to me there should be an easier way like ...<br>
<br>
RcppExport SEXP myfunc(SEXP _n, SEXP _x) {<br>
   const int n=Rcpp::as<int>(_n);<br>
<br>
   double *x=Rcpp::as<double*>(_x); // or a const-correct version<br>
<br>
   double a=fit(x, n);<br>
<br>
...<br>
<br>
Thanks,<br>
<br>
Rodney<br>
<br>
<br>
_______________________________________________<br>
Rcpp-devel mailing list<br>
<a href="mailto:Rcpp-devel@lists.r-forge.r-project.org">Rcpp-devel@lists.r-forge.r-project.org</a><br>
<a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a><br>
</blockquote></div><br><br clear="all"><br>-- <br><div class="gmail_signature">Yixuan Qiu <<a href="mailto:yixuan.qiu@cos.name" target="_blank">yixuan.qiu@cos.name</a>><br>Department of Statistics,<br>Purdue University<br></div>
</div>