[Rcpp-devel] can one modify array in R memory from C++ without copying it?

Darren Cook darren at dcook.org
Sun Nov 6 08:16:12 CET 2011

> If you feel really strongly about you could consider a patch that makes this
> non-R behaviour you suggest an option.  To most of us who use Rcpp between R
> and C++ it really is a feature.

Hello Dirk,
Yes, I'm used to the implicit conversions from other scripting
languages. Rcpp is at the join of languages with different philosophies:
dynamically-typed R and statically-typed C++. But more than that: in R a
very succinct script is regarded as a thing of beauty; in C++ explicitly
describing conversions is regarded as good form by the experts.

This creates some interesting challenges for Rcpp!


P.S. As a realistic example of how implicit conversions turn into bugs:
A developer tests with nice clean csv data, let's say it is stock OHLCV
data, where the values are always integers. An integer vector is passed
into Rcpp code, it modifies memory directly in some subtle way (e.g.
adjusting values down based on data age).

He tests on years worth of data and thousands of symbols and is feeling
very confident. Backtesting on the overall system shows it is making 10%
a year.

This goes into production but one day the program giving the data
changes and now has ".000000" on the end of all the integers.

R now treats it as doubles, Rcpp does an implicit conversion, is no
longer using a pointer and the values no longer get adjusted.
The data still looks plausible, it just hasn't been modified. The next
step, machine learning, goes with this data, and makes different
purchase recommendations. The system is making only 5% a year, but
because it is still making money no-one even realizes something has

Yes, better programmers would have validated their input data better,
would have had more checks, and would have realized the potential for
error. Real programmers on the other hand are human, make mistakes, and
have deadlines.

Darren Cook, Software Researcher/Developer

http://dcook.org/work/ (About me and my work)
http://dcook.org/blogs.html (My blogs and articles)

More information about the Rcpp-devel mailing list