[Rd] Change of variable address due to GC

Tomas Kalibera tomas.kalibera at gmail.com
Fri Sep 8 17:10:07 CEST 2017


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



More information about the R-devel mailing list