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

Slava Razbash slava.razbash at gmail.com
Wed Nov 2 04:32:48 CET 2011


Andre,

You should compare your code with a working RcppArmadillo example,
such as the one on Dirk's website:
http://dirk.eddelbuettel.com/code/rcpp.armadillo.html
"Writing R Extensions" descirbes the .Call() interface and the R API.
When using .Call(), it does not make copies of the objects passed to
it.

Rcpp wraps around the R API so that you program with Rcpp objects.
This simplifies memory management.
In the code that you provided, you should instantiate Rcpp objects first.

Further, if you  #include <RcppArmadillo.h>, then you don't have to
#include <Rcpp.h> or #include <armadillo>.

You could also read Software for data analysis: programming with R by
John Chambers.


Best Regards,

Slava

On Wed, Nov 2, 2011 at 1:38 PM, andre zege <andre.zege at gmail.com> wrote:
> Dirk, apologies, i meant to sent it to R-devel, i just replied to wrong
> list. Now that i read responses on both lists, i am confused. Simon Urbanek
> seemed to indicate that call by reference from
> R to C++ is impossible with .C interface and dangerous and unreliable with
> .Call. If i understood you correctly you seem to say that Rcpp facilitates
> call by reference from R to C++. I actually tried that
> in Rcpp as well but didn't succeed either, may be you could point me in the
> right direction. Here is what i tried
>
>
> modify.cpp
> ===========================
> #include <Rcpp.h>
> #include <RcppArmadillo.h>
> #include <armadillo>
> using namespace Rcpp;
> using namespace arma;
>
> RcppExport void modify(SEXP mem){
>   mat m=as<mat>(mem);
>   m.print();
>   m=m+m;
>
> }
> ==========================
> I compiled the above, pointing  to RccpArmadillo include dir and linked
> shared library to armadillo code. Then i loaded shared library and tried to
> run code from R as follows
>
>
>>dyn.load("/home/az05625/testarma/passptr.so")
>> .Call("modify", m)
>
> matrix m prints out fine, but code segfaults on the last statement m=m+m
> Could you give me some idea on how to fix this?
>
>
>
>
>
>
>> Please ask basic R programming questions on R-devel as you seem to have
>> read
>> the wrong documentation --- there is no support for .C() in Rcpp. We do
>> what
>> we do via SEXP objects, and those require .Call().  So I suggest you read
>> a
>> little more in "Writing R Extensions".  As well as the Rcpp documentation.
>>
>> And as you will learn in the "Rcpp-introduction" and other places, we use
>> what is called proxy model --- so yes, we do pass pointers and no, you
>> don;t
>> get more lightweight than this.
>>
>> Then again, R uses copy-on-write so if you want to truly alter without
>> having
>> R create new copies for you then external pointers are your best bet.
>>
>> Dirk
>>
>> --
>> "Outside of a dog, a book is a man's best friend. Inside of a dog, it is
>> too
>> dark to read." -- Groucho Marx
>
>
>
> _______________________________________________
> Rcpp-devel mailing list
> Rcpp-devel at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
>


More information about the Rcpp-devel mailing list