[Rcpp-devel] Segfaults for a function returning list of vectors of indices

Chris DuBois chris.dubois at gmail.com
Tue Feb 28 19:54:05 CET 2012


Hi all,

Given a vector of integers x = sample(1:K,N,replace=TRUE) with N > K,
I want to return a list where element k is a vector of indices i where
x[i] = k.  (If there is already an R function for this, please let me
know!)  Here is the Rcpp code:

// xr: vector of integers
// mr: max
superwhich <- cxxfunction(signature(xr="integer",Kr="integer"),
'
int K = as<int>(Kr);
Rcpp::IntegerVector x(xr);

// Initialize vector of vectors
std::vector< std::vector<int> > xx;
for (int i = 0; i < K; i++) {
  std::vector<int> tmp(0);
  xx.push_back(tmp);
}

// Push onto appropriate vector for each element of x
for (int i = 0; i < x.size(); i++) {
  xx[x[i]].push_back(i);
}
  return wrap(xx);
', plugin="Rcpp")

For example:
> x <- c(1,3,3,1)
> superwhich(x,4)
[[1]]
integer(0)

[[2]]
[1] 0 3

[[3]]
integer(0)

[[4]]
[1] 1 2


So with large vectors and large values of K, I occasionally get
segfaults.  I believe I have thoroughly checked that I am not making
indexing errors.  I believe I am running into garbage collection
issues.  Isn't wrap() good enough to make R objects?  I could use
Rcpp::List from the start, but is there an equivalent of push_back for
Rcpp::IntegerVectors?  (I've read the documentation, and I apologize
in advance if I am missing something obvious.)

Thanks in advance,
Chris


More information about the Rcpp-devel mailing list