[Rcpp-devel] integer arrays as arguments for a module function

Dirk Eddelbuettel edd at debian.org
Sat Aug 13 02:31:41 CEST 2011


On 12 August 2011 at 16:59, Chris DuBois wrote:
| Point taken: STL looks like the way to go in general.  
| 
| In my particular example, however, the arrays get immediately cast to another
| structure foo (somebody else's code).  So I need to cast from IntegerVector to
| int[] before they get cast to foo[].  What you suggested works perfectly (and
| makes sense in hindsight).
| 
| Is the story identical for char[]?  Where x is a CharacterVector, I tried 
| 
| char* a = x.begin();
| 
| and got
| error: cannot convert ‘Rcpp::Vector<16>::iterator’ to ‘char*’ in
| assignment

char can be a pain. It is something C and C++ didn't get quite right by
lacking a base type for string.  Doing it in plain C (as in *argv[] from
main()) is a pain but doable. 

CharacterVector works as the equivalent of std::vector< std::string >. Out of
each element (ie string) you can extract the underlying char* but you may
have to rely on strcmp etc.  Remember that you may be dealing with pointers
of pointers...  

Have a peek at the unitTests/ directory to see if you find something.

Hope this helps,  Dirk
 
| Any help much appreciated.
| Chris
| 
| On Fri, Aug 12, 2011 at 3:19 PM, Dirk Eddelbuettel <edd at debian.org> wrote:
| 
| 
|     On 12 August 2011 at 14:50, Chris DuBois wrote:
|     | Hi all,
|     |
|     | I'm trying to figure out how to pass in an array of integers to a
|     function
|     | inside a module.  For example, adding the following function to
|     runit.Module.R
|     | works fine:
|     |
|     |         int bla3( IntegerVector x ) {
|     |               return sum(x);
|     |         }
|     |
|     | However, I need to pass an int array, rather than an IntegerVector.
|      Using int
|     | x[] in the arguments doesn't compile (though I'm unfamiliar with C++ in
|     | general, so maybe this shouldn't work anyway).  
|     |
|     | Alternatively, should I just cast x from an IntegerVector to an int
|     array?  I
|     | tried various permutations of as, vector, <int>, etc, and would like to
|     learn
|     | the proper way of doing this.
| 
|     You generally do not want old school x[] arrays in C++. Why?  Because STL
|     vectors do _everything_ they do at (essentially) zero added cost, free you
|     from malloc/free and still allow you to access the straight memory should
|     you
|     need to (to talk to a C API, say).
| 
|     So use IntegerVector for _the interface_. You can the, if you must, do
| 
|         IntegerVector x;
| 
|         int a1[] = x.begin();     // STL-style iterator to beginning of
|     memory
|         int *a2  = x.begin();     // idem
| 
|     Hope this helps,  Dirk
| 
|     --
|     Two new Rcpp master classes for R and C++ integration scheduled for
|     New York (Sep 24) and San Francisco (Oct 8), more details are at
|     http://dirk.eddelbuettel.com/blog/2011/08/04#
|     rcpp_classes_2011-09_and_2011-10
|     http://www.revolutionanalytics.com/products/training/public/
|     rcpp-master-class.php
| 
| 

-- 
Two new Rcpp master classes for R and C++ integration scheduled for 
New York (Sep 24) and San Francisco (Oct 8), more details are at
http://dirk.eddelbuettel.com/blog/2011/08/04#rcpp_classes_2011-09_and_2011-10
http://www.revolutionanalytics.com/products/training/public/rcpp-master-class.php


More information about the Rcpp-devel mailing list