[Rcpp-devel] RcppEigen: .conservativeResize() on 32-bit Windows

Andreas Alfons andreas.alfons at econ.kuleuven.be
Wed Apr 4 15:53:58 CEST 2012

Dear Rcpp devs, dear list,

First of all, thanks a lot for all the effort you put into the Rcpp packages!

I wrote a package using RcppEigen, where in one function I need to
iteratively add and remove vector elements a lot. On 32-bit Windows 7,
the program crashes the R session. It works fine on Ubuntu Oneiric, OS
X Lion (32-bit and 64-bit) and 64-bit Windows 7.

I could narrow the problem down to strange behavior of the function
.conservativeResize(), which I use a lot to resize the vectors. Here
is a minimal example:

test <- cxxfunction(signature(R_p = "integer", R_max = "integer"),
    'int p = as<int>(R_p), max = as<int>(R_max);
        // initialize vector with sequence from 0 to p-1
        Eigen::VectorXi x(p);
        for(int j = 0; j < p; j++) {
            x(j) = j;
        // iteratively remove vector elements
        int k = 0;
        while(k < max) {
            x.segment(0, p) = x.tail(p).eval();  // move last elements up
            x.conservativeResize(p);              // resize vector
        return wrap(x);',
    plugin = "RcppEigen")

test(10, 3)

On 32-bit Windows 7, I get the output
[1] 55862592        2        3        4        5        6        7

The example is not reproducible all the time, sometimes I get the correct output
[1] 3 4 5 6 7 8 9

But restarting the R session once or twice worked for me to get some
incorrect output (not always the same as above). Could the problem be
that the R garbage collector deletes some memory there for some
reason? A standalone C++ version of the example (without going through
R) seems to work fine on 32-bit Windows 7.

Resizing matrices seems to work correctly on 32-bit Windows 7, too, so
maybe I this will provide a workaround for now.

Thanks in advance for your help and sorry for the quite long mail!

- Andreas

Andreas Alfons
Faculty of Business and Economics, KU Leuven

More information about the Rcpp-devel mailing list