2

I have seen other posts regarding this issue. I followed them.

The code I am using to convert a double array to a double vector is-

cppFunction("


             std::vector<double> ArrayToVector(double *arr){

               std::vector<double> vec(arr, arr+ sizeof(arr)/sizeof(arr[0]));

              return vec;
             }



            ")

But I am getting this error:

file52e20f14d34.cpp:28:52: error: cannot initialize a variable of type 'Rcpp::traits::input_parameter<double>::type *' (aka 'InputParameter<double> *') with an lvalue of type 'SEXP' (aka 'SEXPREC *')
    Rcpp::traits::input_parameter< double >::type *arr(*arrSEXP);
                                                   ^   ~~~~~~~~
1 error generated.
make: *** [file52e20f14d34.o] Error 1
clang++  -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG   -I"/Library/Frameworks/R.framework/Versions/3.5/Resources/library/Rcpp/include" -I"/private/var/folders/4w/v4pl36r9475cb2fkspc3qxkm0000gp/T/RtmpSXqByM/sourceCpp-x86_64-apple-darwin15.6.0-1.0.1" -I/usr/local/include   -fPIC  -Wall -g -O2  -c file52e20f14d34.cpp -o file52e20f14d34.o
Error in sourceCpp(code = code, env = env, rebuild = rebuild, cacheDir = cacheDir,  : 
  Error 1 occurred building shared library.

Can anyone explain me why this is happening? Any help would be much appreciated.

S.S. Anne
  • 15,171
  • 8
  • 38
  • 76
gultu
  • 143
  • 10
  • 5
    This is because the pointer has lost the knowledge that it came from an array. `sizeof(arr)` is nonsense when the type is not a fixed array. You can fix this with templates, or by passing either the end of the array of doubles or its length as a second parameter. – Gem Taylor May 14 '19 at 11:12
  • `sizeof(arr)` provide size of the pointer not size of the array it point to. So this is a bug. Since `sizeof(arr[0])` can be bigger then size of pointer then result is zero. – Marek R May 14 '19 at 11:13
  • 1
    This is clearly [XY problem](http://xyproblem.info/). You have some issue with bridging between `R` and `C++` which you didn't described. – Marek R May 14 '19 at 11:15
  • @MarekR But I have complier installed properly! – gultu May 14 '19 at 11:17
  • @GemTaylor I tried putting the length of the array as a second parameter; it did not work either.:( – gultu May 14 '19 at 11:18
  • 4
    error message is completely unrelated to shown code. Please read about XY problem and restate your question. – Marek R May 14 '19 at 11:19
  • @MarekR But I am getting this error! – gultu May 14 '19 at 11:21
  • 3
    Please read at least [Extending R with C++: A Brief Introduction to Rcpp](https://cloud.r-project.org/web/packages/Rcpp/vignettes/Rcpp-introduction.pdf). It is not fair to come here with half-baked question that you expect us volunteers to fix for you _when is there is a decade-worth of documentation and earlier questions_. – Dirk Eddelbuettel May 14 '19 at 11:23
  • "But I am getting this error!" ... but not with the code you posted – 463035818_is_not_an_ai May 14 '19 at 11:28
  • this is not a C question – Curious Student May 14 '19 at 11:30
  • @formerlyknownas_463035818; yes with this code I was getting this error. Anyway, reading the file provided by Dirk – gultu May 14 '19 at 11:36
  • tell me where in included code you see anything related to type: `'Rcpp::traits::input_parameter::type *'` or `'SEXP'`? – Marek R May 14 '19 at 15:58
  • @MarekR The crucial point is `cppFunction()` from the R package [Rcpp](http://dirk.eddelbuettel.com/code/rcpp.html). This function does some code generation and compilation to integrate C++ functions with the R language and works fine for `double` many other argument types, but not for `double*`. – Ralf Stubner May 15 '19 at 12:09
  • @I basically use R vigorously for my analysis. Sometimes I have to use C++ for defining some functions. Anyway, it was a mistake to test some C++ codes in Rcpp. I have to use C++ compiler. – gultu May 15 '19 at 12:23

1 Answers1

3

You cannot infer the length of the array from double* (unless there is a special double value used as an array terminator). The array size must be passed into the function as well:

std::vector<double> ArrayToVector(double* arr, size_t arr_len) {
    return std::vector<double>(arr, arr + arr_len);
}
Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271