[R] .Call function crashes initializing matrix

William Dunlap wdunlap at tibco.com
Sun Apr 11 23:27:55 CEST 2010


> -----Original Message-----
> From: Erik Wright [mailto:eswright at wisc.edu] 
> Sent: Sunday, April 11, 2010 1:59 PM
> To: William Dunlap
> Cc: r help
> Subject: Re: [R] .Call function crashes initializing matrix
> 
> Hi Bill,
> 
> You were right, thanks!
> 
> Now I have:
> 
> int size = 5000;
> double *matrix = (double *) R_alloc(size^2, sizeof(double));

In C the expression
    x ^ y
means the bitwise exclusive-or of x and y, not x to the y power.
Hence size^2 is either size of size-2.  Try using size*size instead.

If you are on Linux you can run R under valgrind
(see wwww.valgrind.org) and valgrind will tell you
when you read or write outside of allocated memory
(or read uninitialized memory).  That is a much more
senstitive test of the code being written correctly
than seeing if it crashes R or not.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com 

> 
> Which works for larger sizes!  But now I have discovered a 
> new problem with it.  When I go to set the value of the 
> elements it *sometimes* crashes:
> 
> for (i = 0; i < (size - 1); i++) {
>     for (j = i; j < (size - 1); j++) {
>         *(matrix + j*length + i) = 5;
>     }
> }
> 
> About ever tenth time I run this code I get:
> 
> R(2535,0xa07f64e0) malloc: *** error for object 0x16830404: 
> incorrect checksum for freed object - object was probably 
> modified after being freed.
> *** set a breakpoint in malloc_error_break to debug
> 
> Any idea why this is happening?
> 
> Thanks again!,
> Erik
> 
> 
> On Apr 10, 2010, at 5:35 PM, William Dunlap wrote:
> 
> >> -----Original Message-----
> >> From: r-help-bounces at r-project.org 
> >> [mailto:r-help-bounces at r-project.org] On Behalf Of Erik Wright
> >> Sent: Saturday, April 10, 2010 3:23 PM
> >> To: r help
> >> Subject: [R] .Call function crashes initializing matrix
> >> 
> >> Hello,
> >> 
> >> I have a C function that I call from R using .Call.  I 
> >> recently discovered a bug in my code, and I am not sure if it 
> >> is a problem with what I am doing in C or if it has something 
> >> to do with my use of .Call.
> >> 
> >> I have narrowed my problem down to these two C statements:
> >> int size = 5000;
> >> double matrix[size][size];
> >> 
> >> When I run this in R it crashes, with this message:
> >> *** caught segfault ***
> >> address 0xb4141d70, cause 'memory not mapped'
> > 
> > You may be overflowing the stack.  Allocate
> > the space for 'matrix' with the allocation
> > functions described in section 5.9 of "Writing
> > R Extensions".  They allocate space on the 'heap',
> > which has much more space than the stack (where
> > space for 'automatic' variables is allocated).
> > Typical stack sizes are on the order of few
> > megabytes and typical heap sizes are in the gigabytes. 
> > 
> > Bill Dunlap
> > Spotfire, TIBCO Software
> > wdunlap tibco.com  
> > 
> >> 
> >> It has no problem when size is smaller, like 50.  Any idea 
> >> what is going on?
> >> 
> >> Thanks!,
> >> Erik
> >> 
> >> 
> >> sessionInfo()
> >> R version 2.10.1 (2009-12-14) 
> >> i386-apple-darwin9.8.0 
> >> 
> >> locale:
> >> [1] en_US.UTF-8/en_US.UTF-8/C/C/en_US.UTF-8/en_US.UTF-8
> >> 
> >> attached base packages:
> >> [1] stats     graphics  grDevices utils     datasets  methods  
> >> [7] base
> >> 
> >> ______________________________________________
> >> R-help at r-project.org mailing list
> >> https://stat.ethz.ch/mailman/listinfo/r-help
> >> PLEASE do read the posting guide 
> >> http://www.R-project.org/posting-guide.html
> >> and provide commented, minimal, self-contained, reproducible code.
> >> 
> 
> 



More information about the R-help mailing list