Re: [Rd] Understanding tracemem

From: Prof Brian Ripley <>
Date: Thu, 12 Jul 2012 18:01:40 +0100

Read the help carefully as to what 'copy' means:

      When an object is traced any copying of the object by the C
      function ‘duplicate’ produces a message to standard output, as
      does type coercion and copying when passing arguments to ‘.C’ or

If you want to understand when 'duplicate' is called, you need to read the source code. File src/main/subassign.c will explain the different paths taken by your two cases. But isn't it rather obvious that duplicating x is not useful when a new longer vector needs to be created?

(BTW, in earlier versions of R tracemem reported some transformations of x to objects of the same length, but not at all consistently.]

On 12/07/2012 17:15, Hadley Wickham wrote:
> Hi all,
> I've been trying to get a better handle on what manipulations lead R
> to duplicate a vector, creating small experiments and using tracemem
> to observe what happens (all in 2.15.1). That's lead me to a few
> questions, illustrated using the snippet below.
> x <- 1:10
> tracemem(x)
> # [1] "<0x1058f8238>"
> x[5] <- 5
> # tracemem[0x1058f8238 -> 0x105994ab0]:
> x[11] <- 11
> Why does x[5] <- 5 create a copy, when x[11] (which should be
> extending a vector does not) ? I can understand that maybe x[5] <- 5
> hasn't yet been optimised to not make a copy, but if that's the case
> then why doesn't x[11] <- 11 make one? I thought it might be because
> somehow tracemem loses track, but adding an additional tracemem(x)
> after x[5] <- 5 doesn't change the output.
> Hadley

Brian D. Ripley,        
Professor of Applied Statistics,
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________ mailing list
Received on Thu 12 Jul 2012 - 17:03:47 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 12 Jul 2012 - 17:40:32 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive