Skip to content

[Rcpp-devel] Vector allocation memory problem

7 messages · Romain Francois, Dirk Eddelbuettel, Cedric Ginestet

#
Hi guys,

I have encountered a reproducible error in using Rcpp, which I have 
reported below. Basically, while allocation of elements in a vector to a 
particular value works well for vectors of small sizes, this returns an 
error when the size of that vector is increased. Either it returns a 
giblc error or it hangs and never resumes. I have reproduced this error 
on two different Linux distributions (Ubuntu 10.10 and 10.04 with g++ 
4.4.1 and R 2.12.1).

####################################################
library(Rcpp)
library(inline)

src <- '
   Rcpp::IntegerMatrix xA(A);
   int n=xA.nrow();
   NumericVector dist(n);
   for(int i=0;i<=n;i++){dist[i]=1.0;}
   return dist;'
cxxfun <- 
cxxfunction(sig=signature(A="matrix"),body=src,plugin="Rcpp",verbose=TRUE)

## Tests.
n <- 20; A <- matrix(0,n,n); dist <- cxxfun(A);
n <- 200; A <- matrix(0,n,n); dist <- cxxfun(A);
n <- 2000; A <- matrix(0,n,n); dist <- cxxfun(A);
*** glibc detected *** /usr/lib/R/bin/exec/R: double free or corruption 
(!prev): 0x08cfc278 ***

####################################################

Is there something that I doing wrong there?
Thank you very much for your help,
Ced
#
Absolutely right!
How stupid of me.
Cheers,
On 08/04/11 13:18, romain at r-enthusiasts.com wrote:
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20110408/7d546f0b/attachment.htm>
#
Hmmm. I think you just need "i<n" instead of "i<=n" in your for construct.

Romain



Le 8 avr. 2011 ? 10:12 AM, Cedric Ginestet <c.ginestet05 at googlemail.com> a ?crit :
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20110408/1b7fb4f6/attachment.htm>
#
On 8 April 2011 at 09:12, Cedric Ginestet wrote:
| Hi guys,
| 
| I have encountered a reproducible error in using Rcpp, which I have reported
| below. Basically, while allocation of elements in a vector to a particular
| value works well for vectors of small sizes, this returns an error when the
| size of that vector is increased. Either it returns a giblc error or it hangs
| and never resumes. I have reproduced this error on two different Linux
| distributions (Ubuntu 10.10 and 10.04 with g++ 4.4.1 and R 2.12.1).
| 
| ####################################################
| library(Rcpp)
| library(inline)
| 
| src <- '
|   Rcpp::IntegerMatrix xA(A);
|   int n=xA.nrow();
|   NumericVector dist(n);
|   for(int i=0;i<=n;i++){dist[i]=1.0;}
|   return dist;'
| cxxfun <- cxxfunction(sig=signature(A="matrix"),body=src,plugin="Rcpp",verbose=
| TRUE)
| 
| ## Tests.
| n <- 20; A <- matrix(0,n,n); dist <- cxxfun(A);
| n <- 200; A <- matrix(0,n,n); dist <- cxxfun(A);
| n <- 2000; A <- matrix(0,n,n); dist <- cxxfun(A);
| *** glibc detected *** /usr/lib/R/bin/exec/R: double free or corruption (!
| prev): 0x08cfc278 ***
| 
| ####################################################
| 
| Is there something that I doing wrong there?

Yup, look like you found a bug. Even with small size (n=20, n=30, n=40)
I end up with the glibc error. Nasty. We'll take a look.

Dirk

| Thank you very much for your help,
| Ced
| 
| 
| --
| Cedric Ginestet
| Centre for Neuroimaging Sciences (L3.04)
| NIHR Biomedical Research Centre
| Institute of Psychiatry, Box P089
| Kings College London
| De Crespigny Park
| London
| SE5 8AF
| 
| ----------------------------------------------------------------------
| _______________________________________________
| Rcpp-devel mailing list
| Rcpp-devel at lists.r-forge.r-project.org
| https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
#
On 8 April 2011 at 14:18, romain at r-enthusiasts.com wrote:
| Hmmm. I think you just need "i<n" instead of "i<=n" in your for construct.

==:-)  

Indeed. I shouldn't mail before coffee.  Here is the fixed version:


library(Rcpp)
library(inline)

src <- '
  Rcpp::NumericMatrix xA(A);
  int n=xA.nrow();
  NumericVector dist(n);
  for(int i=0;i<n;i++){dist[i]=1.0;}
  return dist;'
cxxfun <- cxxfunction(sig=signature(A="matrix"),body=src,plugin="Rcpp")

## Tests.
doWork <- function(n) {
    A <- matrix(0, n, n)
    print(sum(dist <- cxxfun(A)))
    NULL
}

doWork(20)
doWork(200)
doWork(2000)



Dirk
#
Thanks guys,

This is partly why I got confused with this basic mistake. Why don't I 
get a glibc error for all sizes of the vector on my distribution? Shall 
I try to change the default of my g++ compiler or update to a different 
version?

Remember, I am currently using Ubuntu 10.04 with g++ 4.4.1 and R 2.12.1.
Cheers,

Cedric
On 08/04/11 13:36, Dirk Eddelbuettel wrote:
#
On 8 April 2011 at 13:52, Cedric Ginestet wrote:
| Thanks guys,
| 
| This is partly why I got confused with this basic mistake. Why don't I 
| get a glibc error for all sizes of the vector on my distribution? Shall 

Read up on the internet on 'out of bounds access' for arrays etc --- it is
undefined behaviour. Which is what you see.

| I try to change the default of my g++ compiler or update to a different 
| version?
| 
| Remember, I am currently using Ubuntu 10.04 with g++ 4.4.1 and R 2.12.1.

Updating to current versions is always a good idea, but it has no bearing on
your programming error and the related behaviour.

Dirk

| Cheers,
| 
| Cedric
| 
| 
|
| On 08/04/11 13:36, Dirk Eddelbuettel wrote:
| > On 8 April 2011 at 09:12, Cedric Ginestet wrote:
| > | Hi guys,
| > |
| > | I have encountered a reproducible error in using Rcpp, which I have reported
| > | below. Basically, while allocation of elements in a vector to a particular
| > | value works well for vectors of small sizes, this returns an error when the
| > | size of that vector is increased. Either it returns a giblc error or it hangs
| > | and never resumes. I have reproduced this error on two different Linux
| > | distributions (Ubuntu 10.10 and 10.04 with g++ 4.4.1 and R 2.12.1).
| > |
| > | ####################################################
| > | library(Rcpp)
| > | library(inline)
| > |
| > | src<- '
| > |   Rcpp::IntegerMatrix xA(A);
| > |   int n=xA.nrow();
| > |   NumericVector dist(n);
| > |   for(int i=0;i<=n;i++){dist[i]=1.0;}
| > |   return dist;'
| > | cxxfun<- cxxfunction(sig=signature(A="matrix"),body=src,plugin="Rcpp",verbose=
| > | TRUE)
| > |
| > | ## Tests.
| > | n<- 20; A<- matrix(0,n,n); dist<- cxxfun(A);
| > | n<- 200; A<- matrix(0,n,n); dist<- cxxfun(A);
| > | n<- 2000; A<- matrix(0,n,n); dist<- cxxfun(A);
| > | *** glibc detected *** /usr/lib/R/bin/exec/R: double free or corruption (!
| > | prev): 0x08cfc278 ***
| > |
| > | ####################################################
| > |
| > | Is there something that I doing wrong there?
| >
| > Yup, look like you found a bug. Even with small size (n=20, n=30, n=40)
| > I end up with the glibc error. Nasty. We'll take a look.
| >
| > Dirk
| >
| > | Thank you very much for your help,
| > | Ced
| > |
| > |
| > | --
| > | Cedric Ginestet
| > | Centre for Neuroimaging Sciences (L3.04)
| > | NIHR Biomedical Research Centre
| > | Institute of Psychiatry, Box P089
| > | Kings College London
| > | De Crespigny Park
| > | London
| > | SE5 8AF
| > |
| > | ----------------------------------------------------------------------
| > | _______________________________________________
| > | Rcpp-devel mailing list
| > | Rcpp-devel at lists.r-forge.r-project.org
| > | https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
| >