[Rd] reference counting bug: overwriting for loop 'seq' variable

From: William Dunlap <wdunlap_at_tibco.com>
Date: Mon, 01 Jun 2009 18:45:33 -0700


It looks like the 'seq' variable to 'for' can be altered from within the loop, leading to incorrect answers. E.g., in the following I'd expect 'sum' to be 1+2=3, but R 2.10.0 (svn 48686) gives 44.5.

> x = c(1,2); sum = 0; for (i in x) { x[i+1] = i + 42.5; sum = sum +
i }; sum

   [1] 44.5
or, with a debugging cat()s,

> x = c(1,2); sum = 0; for (i in x) { cat("before, i=", i, "\n");
x[i+1] = i + 42.5; cat("after, i=", i,"\n"); sum = sum + i }; sum

   before, i= 1
   after, i= 1
   before, i= 43.5
   after, i= 43.5
   [1] 44.5  

If I force the for's 'seq' to be a copy of x by adding 0 to it, then I do get the expected answer.

> x = c(1,2); sum = 0; for (i in x+0) { x[i+1] = i + 42.5; sum = sum
+ i }; sum

   bbbbb[1] 3

It looks like an error in reference counting.

Bill Dunlap
TIBCO Software Inc - Spotfire Division
wdunlap tibco.com



R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Tue 02 Jun 2009 - 01:49:52 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 02 Jun 2009 - 16:34:40 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive