[Rcpp-devel] Overflow error when creating a large matrix

Qiang Kou qkou at umail.iu.edu
Sun Nov 1 16:23:46 CET 2015

On Sat, Oct 31, 2015 at 3:08 PM, Alessandro Mammana <mammana at molgen.mpg.de>

> Dear All,
> When creating a large matrix with the follwing code:
> #include <Rcpp.h>
> // [[Rcpp::export]]
> Rcpp::IntegerMatrix makeMat(int nrow, int ncol){
>         Rcpp::IntegerMatrix mat(nrow, ncol);
>         return mat;
> }
> I get the error:
> Error in .Primitive(".Call")(<pointer: 0x7f86936d3ea0>, nrow, ncol) :
>   negative length vectors are not allowed
> Where nrow*ncol is a very large number (about 3*10^9). I understand
> that such a number cannot be represented by an int type, but this does
> not seem to be a problem when in R I do:
> > mat <- matrix(0, nrow=nrow, ncol=ncol)
Can you post a reproducible example? I will look into it.

> Questions:
> 1. why is the behaviour different between R and Rcpp in the allocation
> of a matrix?

It should be the same (at least we want to make them same).


2. In a 64 bits machine, what is actually the maximum allowed length
> of a vector/matrix? does the length of a vector/matrix need to be
> represented by an int?

The length limit is R_xlen_t, which is defined in [1]. For matrix, this
limit is for the number of all elements ( nrow * ncol).

Best wishes,


[1] https://github.com/wch/r-source/blob/trunk/src/include/Rinternals.h#L68
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20151101/1f467776/attachment.html>

More information about the Rcpp-devel mailing list