<div dir="ltr"><div>I am exploring the use of some XPtr objects. For example, I can create a pointer to an armadillo matrix. I can then create a copy of that matrix from the pointer within the same function. However, when I try to pass a pointer to a function and create a new copy from said pointer it fails (returns matrix of ~zeros). I feel like I am missing something obvious. Below you will find my code.<br><br></div><div>Regards,<br></div><div>Charles<br></div><div><br></div><div>test.R<br>library(Rcpp)<br>sourceCpp("test.cpp")<br><br>A <- matrix(rnorm(10), 5)<br><br></div># This returns correctly<br><div>testPtr(A)<br><br></div><div># passing a Xptr in to the function returns matrix of ~zeros.<br></div><div>testReturn(testXptr(A), 5, 2)<br><br><br>test.cpp<br>#include <RcppArmadillo.h><br><br>using namespace Rcpp;<br><br>// [[Rcpp::export]]<br>void testPtr(SEXP data){<br>   arma::mat A = as<arma::mat>(data);<br>   int nr = A.n_rows;<br>   int nc = A.n_cols;<br>   <br>   Rcpp::XPtr<double> ptrA(A.memptr());<br>   <br>   arma::mat B = arma::mat( ptrA,<br>         nr,<br>         nc,<br>         true);<br>   B.print("copied matrix");<br>}<br><br>// [[Rcpp::export]]<br>SEXP testXptr(SEXP A)<br>{<br>   arma::Mat<double> armaMat = Rcpp::as<arma::Mat<double> >(A);<br>   Rcpp::XPtr<double> pMat(armaMat.memptr());<br>   return(pMat);<br>}<br><br>// [[Rcpp::export]]<br>void testReturn(XPtr<double> ptrA, int nr, int nc)<br>{<br>   arma::Mat<double> B = arma::Mat<double>( ptrA,<br>         nr,<br>         nc,<br></div><div>         true);<br></div><div>   B.print("copied matrix");<br>}<br></div></div>