[Rcpp-devel] function created from cxxfunction modifies input?

Dirk Eddelbuettel edd at debian.org
Sun Jul 8 02:27:32 CEST 2012


Hi Josh,

On 7 July 2012 at 17:05, Joshua Wiley wrote:
| Thank you so much!  That worked and helped me understand why it was
| happening :)  The idea of pointers is new to me so I think I need to
| spend more time learning about that.

You can think of the Rcpp::* objects as 'proxies' for the R objects. That is
a good thing: imagine a humongous R object---now we don't have to copy it,
but just a pointer to it --> much quicker and efficient. Downside: we're
pointed to the original.

The issue has come up here repeatedly in the past; the first time lead to a
longer thread.  It also just came up again on SO a few days ago:
http://stackoverflow.com/questions/11300048/rcpp-pass-by-reference-vs-by-value/

I have an even more confusing example in the workshop slides: if you use a
NumericVector but then pass an int vector __Rcpp now has to do a deep copy
for you__ as we cannot pass the C++ representation of an int vector to a
float vector so in that case it wouldn't modify your original input either.

Cheers, Dirk

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


More information about the Rcpp-devel mailing list