[Rd] A memory management question

dhinds@sonic.net dhinds at sonic.net
Sun Sep 4 07:42:17 CEST 2005


Can someone explain the use of SETLENGTH() and SETTRUELENGTH()?

I would like to allocate a vector and reserve some space at the end,
so that it appears shorter than the allocated size.  So that I can
more efficiently append to the vector, without requiring a new copy
every time. So I'd like to use SETLENGTH() with a shorter apparent
length, and bump this up as needed until I've used the entire space.

There are only a couple users of SETLENGTH() in R, and they all appear
at first glance to be pointless: a few routines use allocVector() and
then call SETLENGTH() to set the vector length to the value that was
just allocated.  What are valid uses for SETLENGTH()?  And what are
the intended semantics for "truelength" as opposed to the regular
length?

If GC happens and an object is moved, and its apparent LENGTH()
differs from its allocated length, does GC preserve the allocated
length, or the updated LENGTH()?  Is there any way to get at the
original allocated length, given an SEXP?

-- Dave



More information about the R-devel mailing list