通过其内存地址获取对象 [英] Get object by its memory address

查看:83
本文介绍了通过其内存地址获取对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找到在R中创建链接列表的方法.

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

我发现tracemem()返回对象的内存地址,所以有什么办法可以通过对象的内存地址找到对象?

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

推荐答案

那不是做到这一点的方法.如果要引用,请使用引用类或环境.像这样:

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)}

因此,让我们从某些环境开始e:

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

为了证明事情是通过引用完成的,让我们更改e2:

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

> with(e2,{data=555})

e中的下一项也已更改:

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.

试图通过它们的内存位置获取R对象是行不通的.

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

这篇关于通过其内存地址获取对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆