<div dir="ltr">On Tue, Jun 11, 2013 at 10:51 AM, Steve Jaffe <span dir="ltr"><<a href="mailto:sjaffe@riskspan.com" target="_blank">sjaffe@riskspan.com</a>></span> wrote:<br><div class="gmail_extra"><div class="gmail_quote">
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Is there a way to 'wrap', say, an array of double allocated on the heap in C/C++ and return it to R without copying, ie as the data inside a REALSXP?<br>
</blockquote><div><br></div><div style>I don't know of any way of doing that. R is very possessive about memory and expects to be able to garbage-collect any memory allocated in an SEXPREC.</div><div style> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
(Let's say for example that this array allocation is in a 3rd-party library which can't be modified)<br>
<br>
Looking at the 'wrap' code it appears that copying is done in all cases (except for the 'external pointer').<br></blockquote><div><br></div><div style>Yes. The only way to avoid copying is if the external library allows objects to be constructed from pre-allocated memory. This is used in the RcppArmadillo and RcppEigen packages.</div>
<div style> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
Clearly the memory ownership issue is one that would need to be handled. Handing over ownership to R would seem the simplest approach. But it would be more interesting to be able to integrate with something like boost::shared_array so that the lifetime would be correctly managed across both the C and R environments.<br>
<br>
I've read through Rcpp documentation and "Writing R Extensions" without coming up with a good answer, but I'm new to this and I suspect someone familiar with R internals (unlike me) would immediately see what was involved.<br>
<br>
Thanks for your help.<br>
<br>
Steve<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></div></div>