schlather at math.uni-goettingen.de
2007-Oct-10 14:35 UTC
[Rd] documentation of .C (PR#9948)
Full_Name: Martin Schlather Version: R version 2.7.0 Under development (unstable) (2007-10-01 r43043) OS: Linux Submission from: (NULL) (91.3.209.203) Hi, There are 2 dangers with using 'DUP=FALSE' mentioned: * formal arguments * lists Would you also mention a third one, namely that values in R are now only referenced whenever possible and not always copied; hence .C(..., DUP=FALSE) may change the values of other local variables. E.g., with C code void addone(double *x) { *x = *x + 1; } you get x <- as.double(1) y <- x .C("addone", x, PACKAGE="test", DUP=FALSE) print(c(x,y)) #[1] 2 2 x <- as.double(1) y <- as.double(x) .C("addone", x, PACKAGE="test", DUP=FALSE) print(c(x,y)) #[1] 2 2 x <- as.double(1) y <- as.integer(x) .C("addone", x, PACKAGE="test", DUP=FALSE) print(c(x,y)) #[1] 2 1 Many thanks and kind regards, Martin
On Wed, 10 Oct 2007, schlather at math.uni-goettingen.de wrote:> Full_Name: Martin Schlather > Version: R version 2.7.0 Under development (unstable) (2007-10-01 r43043) > OS: Linux > Submission from: (NULL) (91.3.209.203) > > > Hi, > > There are 2 dangers with using 'DUP=FALSE' mentioned: > * formal arguments > * lists > > Would you also mention a third one, namely > that values in R are now only referenced whenever > possible and not always copied; hence .C(..., DUP=FALSE) > may change the values of other local variables. >How about a warning like "if you pass a local variable to .C/.Fortran with DUP=FALSE, your compiled code can alter the local variable and not just the copy in the return list. " -thomas (copied from ?.C, of course)
On Wed, 10 Oct 2007, schlather at math.uni-goettingen.de wrote:> Full_Name: Martin Schlather > Version: R version 2.7.0 Under development (unstable) (2007-10-01 r43043) > OS: Linux > Submission from: (NULL) (91.3.209.203) > > > Hi, > > There are 2 dangers with using 'DUP=FALSE' mentioned: > * formal arguments > * lists > > Would you also mention a third one, namely > that values in R are now only referenced whenever > possible and not always copied; hence .C(..., DUP=FALSE) > may change the values of other local variables.That has always been the case (depending on the meaning of 'possible'), and is part of the first point made. It *does* give a circumstance in which other variables can be changed. Spelling out all of those (and 'local' is not really relevant) would be a mammoth task.> E.g., with C code > void addone(double *x) { *x = *x + 1; } > > you get > > x <- as.double(1) > y <- x > .C("addone", x, PACKAGE="test", DUP=FALSE) > print(c(x,y)) > #[1] 2 2 > > > x <- as.double(1) > y <- as.double(x) > .C("addone", x, PACKAGE="test", DUP=FALSE) > print(c(x,y)) > #[1] 2 2 > > x <- as.double(1) > y <- as.integer(x) > .C("addone", x, PACKAGE="test", DUP=FALSE) > print(c(x,y)) > #[1] 2 1These are the result of changing an actual argument. -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595