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

2009-06-02 Thread Wacek Kusnierczyk
William Dunlap wrote:
 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
b[1] 3

 It looks like an error in reference counting. 
   

indeed;  seems like you've hit the issue of when r triggers data
duplication and when it doesn't, discussed some time ago in the context
of names() etc.  consider:

x = 1:2
for (i in x)
   x[i+1] = i-1
x
# 1 0 1

y = c(1, 2)
for (i in y)
   y[i+1] = i-1
y
# -1 0


vQ

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


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

2009-06-02 Thread luke

Thanks for the report.  Should be fixed in teh devel and 2.9 branches.

luke

On Mon, 1 Jun 2009, William Dunlap wrote:


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
  b[1] 3

It looks like an error in reference counting.

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

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel



--
Luke Tierney
Chair, Statistics and Actuarial Science
Ralph E. Wareham Professor of Mathematical Sciences
University of Iowa  Phone: 319-335-3386
Department of Statistics andFax:   319-335-3017
   Actuarial Science
241 Schaeffer Hall  email:  l...@stat.uiowa.edu
Iowa City, IA 52242 WWW:  http://www.stat.uiowa.edu

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


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

2009-06-01 Thread William Dunlap
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
   b[1] 3

It looks like an error in reference counting. 

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

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel