Rcpp R Code Failure

I have the following C ++ code:

NumericVector testFromontcpp(NumericMatrix z1, NumericMatrix z2, int Nbootstrap){
  int dim1 = z1.nrow();
  int dim2 = z2.nrow();
  int dimension = z1.ncol();
  int N = dim1 + dim2;
  NumericVector TKeps(Nbootstrap+1);
  cout << "toto";
  double bb[N][N];
  cout << "toto";
  return(TKeps);
}

I run it with the package Rcpp: sourceCpp("..."). It works well if z1.size()less than 500. But for higher sizes, it resets and closes R before the second "toto" is printed.

I'd like to know:

  • Am I doing something wrong here?
  • Or is this size problem known in Rcpp?
  • Is there any solution to make my code with z1.size()> 0?

Thanks!

+4
source share
3 answers

This is even worse than Matthew says:

 double bb[N][N];

, C/++ . , , ., , C .

, , :

 Rcpp::NumericMatrix bb(N,N);

++, , Rcpp, malloc/free new/delete.

+10

, , . C99. , . clang -pedantic:

array.cpp:13:15: warning: variable length arrays are a C99 feature [-Wvla-extension]
  double bb[N][N];
              ^
array.cpp:13:12: warning: variable length arrays are a C99 feature [-Wvla-extension]
  double bb[N][N];
       ^

question, .

+8

:

double bb[N][N];

, . .

, std::vector<std::vector<double>>. , . , bb, , .

Related: fooobar.com/questions/944409 / ...

+4
source

Source: https://habr.com/ru/post/1524514/


All Articles