<div dir="ltr"><div><div>Thank you John,<br><br></div>I am familiar with bigmemory (I am one of the current developers actually).  The project I am working on doesn't need the shared memory aspect so was intending to avoid the dependency and just leverage the more familiar and developed Armadillo library.  However your response informs me that I did not fully understand how armadillo objects are handled.  I have some other ideas with how I can address my problem but this was something I was hoping to apply both for this project and for the sake of learning.<br><br></div><div>Regards,<br></div>Charles<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Jun 23, 2015 at 9:29 PM, John Buonagurio <span dir="ltr"><<a href="mailto:jbuonagurio@exponent.com" target="_blank">jbuonagurio@exponent.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi Charles,<br>
<span class=""><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>
</span>armaMat is on the stack, so the Armadillo memptr is no longer valid when you return from the testXptr function.<br>
<br>
One simple solution in your case would be to dynamically allocate with "new" [e.g. arma::mat *A = new arma::mat(...);], though I can't tell you 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 looked into bigmemory? <a href="http://gallery.rcpp.org/articles/using-bigmemory-with-rcpp/" rel="noreferrer" target="_blank">http://gallery.rcpp.org/articles/using-bigmemory-with-rcpp/</a><br>
<span class="HOEnZb"><font color="#888888"><br>
John<br>
<br>
</font></span></blockquote></div><br></div>