Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access object by address / pointer

Tags:

r

data.table

Can I access data.table object created in current R session by its memory address or pointer?

library(data.table)

DT <- data.table(a = 1:10, b = letters[1:10])
address(DT)
# [1] "0x6bf9b90"
attr(DT,".internal.selfref",TRUE)
# <pointer: 0x2655cc8>
like image 806
jangorecki Avatar asked Apr 25 '15 21:04

jangorecki


People also ask

Which pointer is used to access the address of an object?

Access the address of an object using 'this' pointer in C++ In C++, it is allowed to get the address of the object by using 'this' pointer.

How do you access data from pointers?

To access address of a variable to a pointer, we use the unary operator & (ampersand) that returns the address of that variable. For example &x gives us address of variable x.

How do you store address of object in pointer?

Establishing the pointeraddress_of_object() ; cout << "\n\nAddress of example_class object 1 : " << pointer_to_object1; The object1 is used to call the class function address_of_object(). The function returns the address of the calling object. It is stored in the object pointer pointer_to_object1.

How do I find the address of an object?

In order to get the address of an object, from C++11, the template function std::addressof() should be used instead.


1 Answers

This is somewhat of a silly way of doing it (as compared to how you can cast pointers in e.g. C++), but you could do:

# recursively iterate over environments
find.by.address = function(addr, env = .GlobalEnv) {
  idx = which(sapply(ls(env), function(x) address(get(x, env = env))) == addr)
  if (length(idx) != 0)
    return (get(ls(env)[idx], env = env))

  # didn't find it, let's iterate over the other environments
  idx = which(sapply(ls(env), function(x) is.environment(get(x, env = env))))
  for (i in idx) {
    res = find.by.address(addr, get(ls(env)[i], env = env))
    if (res != "couldn't find it") return (res)
  }

  return ("couldn't find it")
}

DT = data.table(a = 1)
e = new.env()
e$DT = data.table(b = 2)
e$f = new.env()
e$f$DT = data.table(c = 2)

find.by.address(address(DT))
#   a
#1: 1
find.by.address(address(e$DT))
#   b
#1: 2
find.by.address(address(e$f$DT))
#   c
#1: 2
like image 53
eddi Avatar answered Oct 09 '22 03:10

eddi