[Rcpp-devel] Passing XPtr between functions

Charles Determan cdetermanjr at gmail.com
Wed Jun 24 15:22:01 CEST 2015


Thank you John,

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.

Regards,
Charles

On Tue, Jun 23, 2015 at 9:29 PM, John Buonagurio <jbuonagurio at exponent.com>
wrote:

> Hi Charles,
>
> > SEXP testXptr(SEXP A)
> > {
> >     arma::Mat<double> armaMat = Rcpp::as<arma::Mat<double> >(A);
> >     Rcpp::XPtr<double> pMat(armaMat.memptr());
> >     return(pMat);
> > }
>
> armaMat is on the stack, so the Armadillo memptr is no longer valid when
> you return from the testXptr function.
>
> 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.
>
> If you're trying to preserve a matrix across function calls in R, have you
> looked into bigmemory?
> http://gallery.rcpp.org/articles/using-bigmemory-with-rcpp/
>
> John
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20150624/0bc827a4/attachment.html>


More information about the Rcpp-devel mailing list