[Rcpp-devel] Irregular crash with rcpp and inline

Dirk Eddelbuettel edd at debian.org
Fri Apr 13 14:17:40 CEST 2012


Hi Marie,

Thanks for posting here.

On 13 April 2012 at 11:31, Marie Auger-Methe wrote:
| 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 

Sign of a memory error writing past bounds...

| 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);
|        }

Here ata is length nc, yet you let j reach length nc --> one past the boundary.

If I change this, all is good as shown below.

| 
|        /////////////////////////////////
|        //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,

The ever-so-slightly modified version (below) works for me with three
repeated calls:

edd at max:~$ r /tmp/marie.r 
            [,1]        [,2]
[1,]  0.00000000  0.00000000
[2,] -0.06557971 -0.03890566
[3,] -0.10972025 -0.04818246
[4,] -0.09398195 -0.11707111
[5,] -0.09527783 -0.13598975
[6,] -0.11354528 -0.13200467
            [,1]        [,2]
[1,]  0.00000000  0.00000000
[2,] -0.06557971 -0.03890566
[3,] -0.10972025 -0.04818246
[4,] -0.09398195 -0.11707111
[5,] -0.09527783 -0.13598975
[6,] -0.11354528 -0.13200467
            [,1]        [,2]
[1,]  0.00000000  0.00000000
[2,] -0.06557971 -0.03890566
[3,] -0.10972025 -0.04818246
[4,] -0.09398195 -0.11707111
[5,] -0.09527783 -0.13598975
[6,] -0.11354528 -0.13200467
Done
edd at max:~$ 


My version is below.

Cheers, Dirk



suppressMessages(library(inline))       # this will include Rcpp for us
suppressMessages(library(CircStats))

mycppfx <- '
  // Input values                       # no need to include cmath again
  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; 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)
print(head(res))
res <- myfx(dist,rta)
print(head(res))
res <- myfx(dist,rta)
print(head(res))

cat("Done\n")


-- 
R/Finance 2012 Conference on May 11 and 12, 2012 at UIC in Chicago, IL
See agenda, registration details and more at http://www.RinFinance.com


More information about the Rcpp-devel mailing list