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

Dirk Eddelbuettel edd at debian.org
Sat Aug 6 13:34:14 CEST 2011


Hi Ulrich,

On 6 August 2011 at 06:40, Ulrich Bodenhofer wrote:
| 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.

What we now all RcppClassic always copied deeply. That's how it was then.

But for the last few years, the new Rcpp has always keept the original SEXP
without copying --- in other words no deep copy unless you call ::clone().

| > [...]
| > See this variant:
| >
| > R>  fun<- cxxfunction(signature(mat="numeric"), plugin="Rcpp", body='
| > +      Rcpp::NumericMatrix matC(mat);
| > +      Rcpp::NumericMatrix matD(mat);
| > +
| > +      matC(1, 1) = 1;
| > +      matD(1, 1) = -matD(1, 1);
| > +
| > +      return(Rcpp::List::create(Rcpp::Named("C")=matC,
| > +                                Rcpp::Named("D")=matD));
| > + ');
| > R>  fun(matrix(42,2,2))
| > $C
| >       [,1] [,2]
| > [1,]   42   42
| > [2,]   42   -1
| >
| > $D
| >       [,1] [,2]
| > [1,]   42   42
| > [2,]   42   -1
| >
| > R>
| >
| > So C and D are really the same thing.
| >
| 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]):
| 
| > test(matrix(1:16, 4, 4))
|       [,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!

It is _the same thing as I just discussed in the emails to Zhongyi. Recall
that you coded a _NumericMatrix_ so if we force a double vector:

R> fun(matrix(seq(1.0, 16.0, by=1.0), 4, 4))
$C
     [,1] [,2] [,3] [,4]
[1,]    1    5    9   13
[2,]    2   -1   10   14
[3,]    3    7   11   15
[4,]    4    8   12   16

$D
     [,1] [,2] [,3] [,4]
[1,]    1    5    9   13
[2,]    2   -1   10   14
[3,]    3    7   11   15
[4,]    4    8   12   16

R> 
 
C and D are once again the same.  Same code as in my previous email.

| 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?

Well, maybe, but it is still goes against decades of C and C++ programming
traditions to access something from outside its scope. 

If you want it to last, create it earlier (in R, as a global, ...).  What you
have here seeems very much like undefined behaviour to me.
 
| Thanks again for your very helpful answers!

Pleasure!

Dirk
 
| Best regards,
| Ulrich
| _______________________________________________
| 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

-- 
Gauss once played himself in a zero-sum game and won $50.
                      -- #11 at http://www.gaussfacts.com


More information about the Rcpp-devel mailing list