[Rcpp-devel] Calling C++ Macros in R using Rcpp

Fatima Batool gbatoolfatima at gmail.com
Sun Aug 28 14:53:52 CEST 2016


Hi all,

Want to ask two basic things. I have a pointer in a macro. How should I
call it in R? Also, in my fun I have a for loop involving pointers to
address of object. I am not sure can I simply put NumericVector here or
there is some other things to take care of? Below is the code.


#include <Rcpp.h>

using namespace Rcpp;

#define initilizer(filler, fillee, length) \

do { int counter; \

for(counter = 0; counter < (length); *((fillee)+ counter++)=(filler)); \

} \

while(0);

using namespace Rcpp;

void fun(int n, NumericVector array_1, double *object){

    int i;

    initilizer(1.1, array_1, n);

    for (i = 0, *object = 0.; i < n; *object += array_1[i++]);

}


R code:

install.packages("Rcpp")

library(Rcpp)

sourceCpp("stackflow.cpp")


Errors are:


##########################################################


clang++ -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG  -I/usr
/local/include -I/usr/local/include/freetype2 -I/opt/X11/include  -I
"/Library/Frameworks/R.framework/Versions/3.3/Resources/library/Rcpp/
include" -I"/Users/fatimabatool/Documents/New/R codes"    -fPIC  -Wall -
mtune=core2 -g -O2  -c stackflow.cpp -o stackflow.o

Error in sourceCpp("stackflow.cpp") :

  Error 1 occurred building shared library.

stackflow.cpp:18:5: error: indirection requires pointer operand ('typename
traits::enable_if<traits::is_convertible<typename
traits::remove_const_and_reference<int>::type,
typename traits::storage_type<14>::type>::value, typename
sugar::Plus_Vector_Primitive<14, true, Vector<14, PreserveStorage> >
>::type' (aka 'Rcpp::sugar::Plus_Vector_Primitive<14, true,
Rcpp::Vector<14, PreserveStorage> >') invalid)

    initilizer(1.1, array_1, n);

    ^~~~~~~~~~~~~~~~~~~~~~~~~~~

stackflow.cpp:12:38: note: expanded from macro 'initilizer'

for(counter = 0; counter < (length); *((fillee)+ counter++)=(filler)); \

                                     ^~~~~~~~~~~~~~~~~~~~~~

1 error generated.

make: *** [stackflow.o] Error 1

#############################################################


 Below is the C++ code.


#include <iostream>

using namespace std;

#define initilizer(filler, fillee, length) \

do { int counter; \

for(counter = 0; counter < (length); *((fillee)+ counter++)=(filler)); \

} \

while(0);


void fun(int n, double *array_1, double *object){

    int i;

    initilizer(1.1, array_1, n);

    for (i = 0, *object = 0.; i < n; *object += array_1[i++]);

    return;

}


int main()

{

    int n = 5;

    double *array_1;

    double object;

    array_1 = (double*)calloc(n, sizeof(double));

    fun(n, array_1, &object);



    for(int i = 0; i < n; i++){

        cout << "array_1[" << i << "]=" << array_1[i] << endl;

    }

    cout << "object = " << object << endl;



}



The output is:


array_1[0]=1.1

array_1[1]=1.1

array_1[2]=1.1

array_1[3]=1.1

array_1[4]=1.1

object = 5.5


Many Thanks

Kindest Regards

Fatima
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20160828/98120ce8/attachment-0001.html>


More information about the Rcpp-devel mailing list