[Rcpp-devel] Preferred idiom for obtaining a pointer in Rcpp

Romain Francois romain at r-enthusiasts.com
Tue Mar 16 08:43:52 CET 2010


Le 16/03/10 01:33, Douglas Bates a écrit :
>
> I have been using the begin method to obtain a pointer to the contents
> of an Rcpp::Vector object.  For example
>
> Rcpp::NumericVector foo(10);
> double *ptr = foo.begin();
>
> Would a preferred idiom be
>
> double *ptr =&foo[0]:
>
> In C I would use the REAL function to obtain a pointer to the contents
> of the R object.

Those three options are identical, although we cache the pointer so that 
we don't have to call REAL each time, as we found this can be expensive.

I prefer begin, at least for readability (I have never really been 
comfortable with the &).

You often see people using &v[0] with std::vector because in that case 
those are not identical, for example the gcc implementation of 
vector::iterator is not a raw pointer so vector::begin() does not return 
a pointer, but &x[0] does.

Also, for not simple vectors (character, generic, expression), you 
really have to go through with iterators because we cannot use pointers 
here (write barrier).

Romain

-- 
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://tr.im/OIXN : raster images and RImageJ
|- http://tr.im/OcQe : Rcpp 0.7.7
`- http://tr.im/O1wO : highlight 0.1-5



More information about the Rcpp-devel mailing list