[R] R: possibility for sth. like parameter passing by reference?
ihaka at stat.auckland.ac.nz
Tue Feb 1 20:28:29 CET 2000
From owner-r-help at stat.math.ethz.ch Wed Feb 2 06:36:50 2000
To: Thomas Lumley <thomas at biostat.washington.edu>
Cc: Marcus Eger <marcus.eger at physik.uni-marburg.de>,
"r-help at stat.math.ethz.ch" <r-help at stat.math.ethz.ch>
Subject: Re: [R] R: possibility for sth. like parameter passing by reference?
From: Peter Dalgaard BSA <p.dalgaard at biostat.ku.dk>
Date: 01 Feb 2000 18:35:07 +0100
Thomas Lumley <thomas at biostat.washington.edu> writes:
> On Tue, 1 Feb 2000, Marcus Eger wrote:
> > Hello,
> > is there a possibility to pass parameters to a
> > function without copying them into the new environment?
> For example, if I define x to be a really big matrix and then
> R> g<-function(x) mean(x)+1
> R> f<-function(x) mean(x+1)
> R> g(w)
>  0.9992614
> R> f(w)
> Error: heap memory (6144 Kb) exhausted [needed 1562 Kb more]
> The first function doesn't copy x, the second does.
Interesting... I would have expected that both functions would copy x
when the argument is evaluated, but the 2nd one create an additional
object the same size as x to hold the intermediate value of x+1.
We put quite a bit of work into making this happen. I would describe the
semantics as "copy on modify (if necessary)". Copying is done only when
objects are modified. The (if necessary) part means that if we can prove
that the modification cannot change any non-local variables then we just go
ahead and modify without copying.
For example, log(x+1) creates a copy when "x+1" is evaluated, but no
additional copy is made when log() is evaluated -- the result is written
back into the same bit of memory that x+1 was sitting in. This is ok
because there is no name bound to the value of "x+1".
1. This is how it used to be -- the present state may differ :-).
2. I now a firm believer in call-by-reference semantics.
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
More information about the R-help