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

andre zege andre.zege at gmail.com
Wed Nov 2 03:38:34 CET 2011


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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20111101/eee2c418/attachment.htm>


More information about the Rcpp-devel mailing list