[Rcpp-devel] Irregular crash with rcpp and inline
Marie Auger-Methe
marie.augermethe at gmail.com
Fri Apr 13 12:31:29 CEST 2012
Hi,
I am new to Rcpp, c++, and inline. I have created a simple test function
which works some of the times but not all the times. It generally
crashes when I run it with a large sample size or after calling the
function multiple times. When it doesn't work, it crashes R completely
and so I have no error message that can help me figure out what caused
the problem.
Here is my code:
library(inline)
library(Rcpp)
library(CircStats)
mycppfx <- '
#include <cmath>
// Input values
NumericVector distc(dist);
NumericVector rtac(rta);
int nc = distc.size();
NumericVector dx(nc);
NumericVector dy(nc);
NumericMatrix coord(nc+1,2);
// Transforming relative turning angle into absolute turning angle
NumericVector ata(nc);
ata[0] = fmod(rtac[0],2*PI);
for(int j = 1; j < nc+1; j++){
ata[j] = fmod(ata[j-1] + rtac[j],2*PI);
}
/////////////////////////////////
//Change into cartesian coordinates
// Calculate the displacement in each direction
dx = distc * cos(ata);
dy = distc * sin(ata);
nc += 1;
// Add the displacments
for(int j = 1; j < nc; j++){
coord(j,0) = coord(j-1, 0) + dx[j-1];
coord(j,1) = coord(j-1, 1) + dy[j-1];
}
return coord;
'
myfx <- cxxfunction(signature(dist ="NumericVector",
rta="NumericVector"),
body = mycppfx, plugin = "Rcpp")
n <- 1000
dist <- rexp(n,10)
rta <- rvm(n,0,1)
res <- myfx(dist,rta)
Any tips would be appreciated,
Marie
More information about the Rcpp-devel
mailing list