[Rcpp-devel] Operating on sparse matrices in a list.

Kumar MS mskb01 at gmail.com
Wed Dec 11 16:40:27 CET 2024


Hello Rcpp developers,

The following simple code attempts to instantiate every sparse matrix in a
given R list of sparse matrices ("dgCMatrix"). The code compiles OK, but
crashes at runtime with a segmentation fault.

I followed the as<> approach outlined in
https://gallery.rcpp.org/articles/dynamic-dispatch-for-sparse-matrices/ ,
although in that post, we are not dealing with a list of sparse matrices,
but with just one sparse matrix.

Thank you, SK.

library(Rcpp)library(Matrix)

# -- The function in question --cppFunction(
  "arma::mat accessEntries(Rcpp::List x){
     for(int i=0; i<x.size(); i++){
     arma::sp_mat ar=Rcpp::as<arma::sp_mat>(x[i]); //similar to :
https://gallery.rcpp.org/articles/dynamic-dispatch-for-sparse-matrices/
     }
  }",
depends=c("RcppArmadillo", "Rcpp"),
plugins = "cpp11"
)

# -- Construct a list of sparse matrices and call the above function --
X <- as(cbind( c(1,2,3), c(3,4,5), c(5, 6, 7) ), "dgCMatrix")
spList <- list( X, X )accessEntries(spList)

# -- Output --
# *** caught segfault ***#address 0x0, cause 'unknown'

#Traceback:
# 1: .Call(<pointer: 0x10df0eba0>, x)
# 2: accessEntries(spList)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20241211/7fa693e6/attachment.htm>


More information about the Rcpp-devel mailing list