Rcpp code crashes R

c++, r, rcpp

Solution

It is even worse than Matthew says as

 double bb[N][N];

is simply wrong as C / C++ have no native two-dimensional structure. You always create a long vector and then use clever indexing into it, see e.g. the old Numerical Recipes in C code for emulating matrices.

Here, this is plain silly as we do have matrix types, so use one:

 Rcpp::NumericMatrix bb(N,N);

The bigger isue that with modern C++, as well as the classes provided by Rcpp, you should never have to use `malloc`/`free` or `new`/`delete`.

Problem

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 Rcpp package : `sourceCpp("...")`. It works well if `z1.size()` is under 500. But for higher sizes, it crashes and closes R before the second "toto" is printed. I would like to know : - Am I doing something wrong here ? - Or is this issue of size in Rcpp known ? - Is there a solution to make my code run with `z1.size()` >0 ? Thank you !

Original source

Related problems