Skip to content

Instantly share code, notes, and snippets.

@alekrutkowski
Last active February 22, 2024 16:22
Show Gist options
  • Save alekrutkowski/c14669c80523ca279dff3623fca53ee7 to your computer and use it in GitHub Desktop.
Save alekrutkowski/c14669c80523ca279dff3623fca53ee7 to your computer and use it in GitHub Desktop.
Get memory address of an R object
memoryAddress <- function(x)
strsplit(capture.output(.Internal(inspect(x))),split=" ")[[1]][1]
### Usage examples
# > abc <- c(1,2,3,10,11)
# > xyz <- abc
# > memoryAddress(abc)==memoryAddress(xyz)
# [1] TRUE
# > xyz <- abc+1
# > memoryAddress(abc)==memoryAddress(xyz)
# [1] FALSE
# > abc <- c(1,2,3,10,11)
# > xyz <- abc
# > memoryAddress(abc)==memoryAddress(xyz)
# [1] TRUE
# > xyz <- unserialize(serialize(abc,NULL)) # "deep copy" hack
# > memoryAddress(abc)==memoryAddress(xyz)
# [1] FALSE
# > identical(abc,xyz)
# [1] TRUE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment