From simon.urbanek at r-project.org  Thu Mar 15 20:14:14 2007
From: simon.urbanek at r-project.org (Simon Urbanek)
Date: Thu, 15 Mar 2007 15:14:14 -0400
Subject: [Rd] allocVector reference
In-Reply-To: <20070315120149.GA68701@tehun.pair.com>
References: <946928E281E8FD4B91D5B2CC3C91967DAA9912@lnex01.CONCORDIAFUNDS.COM>
	<20070315120149.GA68701@tehun.pair.com>
Message-ID: <1C4063EA-8EF9-4373-ABE2-38337150A8CD@r-project.org>

Andrew,

On Mar 15, 2007, at 8:01 AM, Andrew Piskorski wrote:

> On Wed, Mar 14, 2007 at 05:44:04PM -0000, Matthew Dowle wrote:
>>
>> Hi,
>>
>> I'm trying to write a function to return an R vector which points  
>> directly to a contiguous subset of another vector, without taking  
>> a copy.
>
> Matthew, I don't know the answer to your question, but this all  
> seems related to support for references in R.  I've included my  
> notes on R references below.
> [...]
>> Since SEXPREC is a header followed by the data, rather than the  
>> header plus a pointer to the data, I'm not sure what I'm trying to  
>> do is possible.  Is there a way of doing this?  Similar in spirit  
>> to how the R assignment "x=y" does not copy y until x is modified,
>
> Is you last statement above in fact true?


Yes. Just try
a=1:100000000
gc()
b=a
gc()
b[0]=0:0
gc()


>   I was under the impression that R does NOT do copy on write, that  
> when you make a copy of a variable, R immediately allocates memory  
> and makes a deep copy of the value.
>

Nope.


> But you're using old deprecated "=" for assignment,

"old deprecated"? It was introduced in 1.4.0 and it is the most  
recently introduced assignment operator in R. I think that you are  
mistaking it for "_" which is no assignment operator anymore but used  
to be.


> which is weird, so maybe you mean when you pass named arguments to  
> a function?  Function args are evaluated lazily, and I think that  
> is used (I don't know how exactly) to give copy on write behavior -  
> but only for function arguments.


Cheers,
Simon



[Rd] allocVector reference
Simon Urbanek