The Rcpp version now works. However, there is some memory allocation problem that crashes R when the garbage collection is performed. It occurs for both versions in R and Rcpp.