[Rcpp-devel] returning array from C

Dirk Eddelbuettel edd at debian.org
Tue Jun 11 18:18:24 CEST 2013


On 11 June 2013 at 10:51, Steve Jaffe wrote:
| 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?
| 
| (Let's say for example that this array allocation is in a 3rd-party library which can't be modified)

It sounds like you may want Rcpp::XPtr -- our wrapper class around R's
external pointers.

| Looking at the 'wrap' code it appears that copying is done in all cases (except for the 'external pointer').

Yep.
 
| 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.

It is tricky.  

R really wants to control its memory, and you're either "with it" or not.

I also have some projects with oodles of boost::shared_ptr data and am not
yet sure how best to do this.  The safest yet most expensive way is probably
to copy it.  Or to go via XPtr.
 
| 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.

There may not be a better answers.

As you know, R really is an interpreted system with an API for extensions.
We are using the latter.  But R always assumes it has all the memory and can
(and should) do the management -- hence the recommendation in "Writing R
Extension" to not mix its allocator with external allocators.

That recommendation has worked well for us with Rcpp.  There are other use
cases, and it is not yet clear how to integrate them.

Dirk

-- 
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com


More information about the Rcpp-devel mailing list