[Rd] reference counting problem in .Primitive's?

William Dunlap
Thu, 23 Apr 2009 17:06:38 -0000

I think the following rather wierd expressions show a problem in how
some of the .Primitive functions evaluate their arguments.  I haven't
yet thought of a way that a nonabusive user might run into this problem.
In each case the first argument, x, is modified in the course of
evaluating the second argument and then modified x gets used
as the first argument:

> x<-as.integer(1:5); y <- x + { x[3]<-33L ; 1L } ; y
[1]  2  3 34  5  6
> x<-2^(0:4) ; y <- log(x, { x[3]<-64 ; 2 }) ; y
[1] 0 1 6 3 4

The reason I think it looks like a sharing problem (and not an order
of evaluation problem) is that if your modification to x causes it to
use a new block of memory then the unmodified version of x gets
used as the first argument.  E.g.,

> x<-as.integer(1:5) ; y <- x + { x[3]<-33.3; 1L} ; y
[1] 2 3 4 5 6

I haven't yet thought of a way that a nonabusive user might run
into this problem.

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