Skip to content
Prev 2701 / 10988 Next

[Rcpp-devel] Some questions regarding internals of Rcpp objects

Dear Dirk,

Thanks for your detailed comments!

Regarding the first question: as I wrote, I was actually assuming that 
Rcpp would not make deep copies.
Fine. If so, I actually wonder what's wrong with my function

   RcppExport SEXP test(SEXP mat)
   {
   BEGIN_RCPP
       Rcpp::NumericMatrix matC(mat);
       Rcpp::NumericMatrix matD(mat);

       matC(1, 1) = 1;
       matD(1, 1) = -matD(1, 1);

       return(matC);
   END_RCPP
   }

I tried it on matrix(42, 2, 2) too and, surprise, it returns -1 as 
element [2, 2] - just as in your example. So far so good. However, if I 
apply it to matrix(1:16, 4, 4) (and this is an example similar to the 
one I tried before, which actually made me write this question to the 
list), I get the following (note the 1 at [2, 2]):
[,1] [,2] [,3] [,4]
[1,]    1    5    9   13
[2,]    2    1   10   14
[3,]    3    7   11   15
[4,]    4    8   12   16


If it's not the copying, what else is wrong here? I actually don't get 
it. Very mysterious!

Regarding my third question, I have to admit that my example was flawed. 
Stupid me! I wanted to write something like the following:

   double *p;

   {
      Rcpp::NumericVector vec(10);
      p = vec.begin();
   }

As I understand your reply, I could use p outside the block, as it still 
points to the data inside the R object created inside the block, right?

Thanks again for your very helpful answers!

Best regards,
Ulrich