Message-ID: <CAJ_LAMB6Bpz12FJT75HDFYQqSHa3jZbYp3b7DOKv66K=gfeogw@mail.gmail.com>
Date: 2015-11-01T15:23:46Z
From: Qiang Kou
Subject: [Rcpp-devel] Overflow error when creating a large matrix
In-Reply-To: <CA+RJ7GMvPu0W8B2+p+wTu4NJ5YhcGoNrXrLxpHpNedB_9DViSw@mail.gmail.com>
On Sat, Oct 31, 2015 at 3:08 PM, Alessandro Mammana <mammana at molgen.mpg.de>
wrote:
> 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,
KK
[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>