Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get object by its memory address

Tags:

memory

r

I am trying to find ways to make link list in R.

I found tracemem() returns the memory address of an object, so is there any way I can find an object by its memory address?

like image 380
user2961927 Avatar asked Feb 23 '14 07:02

user2961927


1 Answers

That's not the way to do it. If you want references use Reference Classes or environments. Like this:

First, three objects I am going to put in my linked list:

> e1=new.env()
> e2=new.env()
> e3=new.env()

initialise with a data item and a pointer to the next in the list

> with(e1,{data=99;nextElem=e2})
> with(e2,{data=100;nextElem=e3})
> with(e3,{data=1011;nextElem=NA})

now a function that given an environment returns the next in the linked list:

> nextElem = function(e){with(e,nextElem)}

So lets start from some environment e:

> e=e1
> with(e,data)
[1] 99

To get the value of the next item in the list:

> with(nextElem(e),data)
[1] 100

and just to prove things are being done by reference, lets change e2:

> with(e2,{data=555})

and the next item from e has changed too:

> with(nextElem(e),data)
[1] 555

Reference classes should make this a bit cleaner, but require a bit of planning.

Trying to get R objects by their memory location is not going to work.

like image 185
Spacedman Avatar answered Oct 07 '22 13:10

Spacedman