<div dir="ltr"><div>The fastLm examples are nice but they don't contain anything relating to passing an XPtr between functions.  Regarding your comment about making an XPtr around arma::mat 'innards' I'm not sure I fully understand.  I tried creating an XPtr around a arma::mat but no success on passing the object between functions.  It still returns that ~zeros.<br><br>// [[Rcpp::export]]<br>SEXP testXptr(SEXP A)<br>{<br>    arma::Mat<double> armaMat = as<arma::mat>(A);<br>    XPtr<arma::mat> pMat(&armaMat);<br>    return(pMat);<br>}<br><br>// [[Rcpp::export]]<br>void testReturn(SEXP ptrA, int nr, int nc)<br>{<br>    XPtr<arma::mat> ptrB(ptrA);<br>    arma::mat B = arma::mat( (double *) ptrB->memptr(),<br>          nr,<br>          nc,<br>          false);<br>    B.print("copied matrix");<br>}<br><br></div>Charles<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Jun 24, 2015 at 8:32 AM, Dirk Eddelbuettel <span dir="ltr"><<a href="mailto:edd@debian.org" target="_blank">edd@debian.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class=""><br>
On 24 June 2015 at 08:22, Charles Determan wrote:<br>
| Thank you John,<br>
|<br>
| I am familiar with bigmemory (I am one of the current developers actually). <br>
| The project I am working on doesn't need the shared memory aspect so was<br>
| intending to avoid the dependency and just leverage the more familiar and<br>
| developed Armadillo library.  However your response informs me that I did not<br>
| fully understand how armadillo objects are handled.  I have some other ideas<br>
| with how I can address my problem but this was something I was hoping to apply<br>
| both for this project and for the sake of learning.<br>
<br>
</span>Look at more current RcppArmadillo examples, and in particular _all_ the<br>
variants of the fastLm benchmark in examples.  By doing the explicit cast,<br>
you _forced_ a copy which may then have destroyed the Xptr link.  I *think*<br>
you could just do XPtr around arma::mat innards.  I would actually be<br>
interested in having something somewhere between bigmemory (where I was one<br>
of the first external users) and what we have in RcppArmadillo.<br>
<br>
Dirk, during a break of Rcpp class in Zuerich<br>
<span class=""><br>
| Regards,<br>
| Charles<br>
|<br>
| On Tue, Jun 23, 2015 at 9:29 PM, John Buonagurio <<a href="mailto:jbuonagurio@exponent.com">jbuonagurio@exponent.com</a>><br>
| wrote:<br>
|<br>
|     Hi Charles,<br>
|<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>
|     armaMat is on the stack, so the Armadillo memptr is no longer valid when<br>
|     you return from the testXptr function.<br>
|<br>
|     One simple solution in your case would be to dynamically allocate with<br>
|     "new" [e.g. arma::mat *A = new arma::mat(...);], though I can't tell you<br>
|     off hand how object lifetime is managed with Armadillo objects.<br>
|<br>
|     If you're trying to preserve a matrix across function calls in R, have you<br>
|     looked into bigmemory? <a href="http://gallery.rcpp.org/articles/" rel="noreferrer" target="_blank">http://gallery.rcpp.org/articles/</a><br>
|     using-bigmemory-with-rcpp/<br>
|<br>
|     John<br>
|<br>
|<br>
|<br>
</span>| _______________________________________________<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" rel="noreferrer" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a><br>
<div class="HOEnZb"><div class="h5"><br>
--<br>
<a href="http://dirk.eddelbuettel.com" rel="noreferrer" target="_blank">http://dirk.eddelbuettel.com</a> | @eddelbuettel | <a href="mailto:edd@debian.org">edd@debian.org</a><br>
</div></div></blockquote></div><br></div>