[R] stack overflow

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Fri Sep 5 23:19:50 CEST 2003


William Noble <noble at gs.washington.edu> writes:

> Hello,
> 
> I am trying to do an ANOVA on a microarray data set consisting of
> 22690 elements.  The ANOVA is fine, but when I try to put the data in
> a frame in order to exporting it, I get a stack overflow.  I have
> found documentation on dynamic memory in R, but not on how to increase
> the stack size.  The code I'm using is below.  If anyone has any
> suggestions for a workaround here, I'd appreciate it.

You might want to consider turning it into a matrix instead (just use
sapply()). 

However, this looks like a bug and it has even got worse in r-devel.
So thanks for drawing attention to it.

To paraphrase the situation, just take a long list of short vectors
and turn it into a data frame:

> tmp <- lapply(1:22690,function(i)rnorm(3))
> xx <- data.frame(tmp)
Segmentation fault

The problem comes from within the error handler itself. 

Program received signal SIGSEGV, Segmentation fault.
Rf_errorcall (call=0x81dadb0, format=0x817c598 "protect(): stack
overflow")
    at ../../../R/src/main/errors.c:481
481         vsignalError(call, format, ap);

The traceback indicates that Rf_protect() goes into infinite
recursion. (Luke?) 


Deep  down in the stack we have 

#760082 0x08073e48 in Rf_substituteList (el=0x8d47a9c, rho=0x81dadb0)
    at ../../../R/src/main/coerce.c:1811
1811            PROTECT(h = substitute(CAR(el), rho));
(gdb) down
#760081 0x080bf717 in Rf_protect (s=0x978a050)
    at ../../../R/src/main/memory.c:1999
1999            errorcall(R_NilValue, "protect(): stack overflow");

and below that we have

#760082 0x08073e48 in Rf_substituteList (el=0x8d47a9c, rho=0x81dadb0)
    at ../../../R/src/main/coerce.c:1811
1811            PROTECT(h = substitute(CAR(el), rho));
(gdb)
#760083 0x08073e54 in Rf_substituteList (el=0x8d47ab8, rho=0x81dadb0)
    at ../../../R/src/main/coerce.c:1812
1812            PROTECT(t = substituteList(CDR(el), rho));
(gdb)
#760084 0x08073e54 in Rf_substituteList (el=0x8d47ad4, rho=0x81dadb0)
    at ../../../R/src/main/coerce.c:1812
1812            PROTECT(t = substituteList(CDR(el), rho));
(gdb)
#760085 0x08073e54 in Rf_substituteList (el=0x8d47af0, rho=0x81dadb0)
    at ../../../R/src/main/coerce.c:1812
1812            PROTECT(t = substituteList(CDR(el), rho));

So the original problem is that substituteList() is not happy with
long lists. I'm not really sure but my gut feeling is that the tail
should be computed before the head (i.e. reverse lines 1811 and 1812)
so that you don't end up with a pile of heads computed before the
recursion ends.

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907




More information about the R-help mailing list