From tomas.kalibera at gmail.com  Fri Sep  8 17:10:07 2017
From: tomas.kalibera at gmail.com (Tomas Kalibera)
Date: Fri, 8 Sep 2017 17:10:07 +0200
Subject: [Rd] Change of variable address due to GC
In-Reply-To: <trinity-8a6a4fc3-add7-4837-9b62-f488286205e9-1504879681153@3c-app-mailcom-bs14>
References: <trinity-8a6a4fc3-add7-4837-9b62-f488286205e9-1504879681153@3c-app-mailcom-bs14>
Message-ID: <ceee71d9-801a-bd3c-6013-74ae427e4c47@gmail.com>


I think you might get a more useful answer if you say what you want to 
achieve.

"address(x)" does not give you an address of variable "x" but of an 
object bound to x. The GC in R is non-moving, it does not relocate 
objects. However, a number of things can happen that will change the 
binding of "x" to point to a (modified) copy of the original object 
(such as replacement operations like x[3] <- 4 or class(x) <- "foo"). 
When these copies are made is implementation dependent and may change 
between R versions or may become unpredictable to the R program. An R 
program should only depend on values stored in an object, not on the 
location of that object, and this is also why "address(x)" is not part 
of the base packages.

Best
Tomas

On 09/08/2017 04:08 PM, lille stor wrote:
> Hi,
>   
> I would like to know if the Garbage Collector (GC) changes the address of a variable in R. In other words, assuming the following code:
>   
>        library(pryr)
>   
>        x <- 1:1024
>   
>        addr <- address(x)  # save address of variable "x" in "addr"
>
>                .
>                .
>                .
>        (execution of operations that create/destroy many small/big objects in memory, which will likely make the GC to be called)
>                .
>                .
>                .
>   
>        if (addr != address(x))
>        {
>            print("Address of x changed!")
>        }
>   
>   
> Will the message "Address of x changed!" be ever printed?
>   
> Thank you!
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel



[Rd] Change of variable address due to GC
Tomas Kalibera