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
.