“retrcemem”在R中做了什么?

时间:2011-09-07 01:37:09

标签: r internals

在回答了tracemem recently后,我了解了retracemem?retracemem的帮助及其中的例子让我无法理解。

retracemem实际上做了什么以及为什么?它没有按照我的想法做到,即将一个对象指向另一个对象的内存位置,至少就我所知。

我尝试了像.Internal(inspect())这样的向导调用,这对我来说没有任何魔力:

> a = 1:10
> b = a[-1]
> .Internal(inspect(a))
@00000000087AE578 13 INTSXP g0c4 [NAM(2)] (len=10, tl=23336) 1,2,3,4,5,...
> .Internal(inspect(b))
@00000000087AE8E8 13 INTSXP g0c4 [NAM(2)] (len=9, tl=7208) 2,3,4,5,6,...
> retracemem(b,retracemem(a))
> .Internal(inspect(b))
@00000000087AE8E8 13 INTSXP g0c4 [NAM(2)] (len=9, tl=7208) 2,3,4,5,6,...

1 个答案:

答案 0 :(得分:1)

我认为retracemem()只是让你标记一个变量副本,它不会产生一个tracemem语句(例如上面的b,它实际上只是a的一个副本。第一个元素),因为它是从相同的源变量派生的,所以你可以继续观察传播的副本/部分副本,看看它们是从同一个源派生的。

例如,如何复制/传播A的内存:

> A <- 1:10
> tracemem(A)
[1] "<0x100a2a978>"
> B <- A                                # Assignment to B doesn't make copy
> C <- A + 1                            # Assignment to C makes copy, alters it
tracemem[0x100a2a978 -> 0x1020ebbf0]: 
> D <- C + 1                            # Assignment to D makes copy, alters it
tracemem[0x1020ebbf0 -> 0x1020ebc98]: 
> E <- B + 1                            # Assignment to E makes copy, alters it
tracemem[0x100a2a978 -> 0x1020a4208]: 
> F <- A[-1]                            # Assignment to F doesn't make copy?
> G <- F + 1                            # Even after altering it?
> retracemem(F, retracemem(A))          # Hint to R that F is really A derived
tracemem[<0x100a2a978> -> 0x1009c5910]: 
> G <- F + 1                            # Reassignment to G makes copy, alters it
tracemem[0x1009c5910 -> 0x1020a4748]: 

但话又说回来,我可能完全错了......