Re: [Rd] append/concatenate an element to a list in C-language

From: Robert Castelo <>
Date: Fri, 19 Oct 2007 11:08:47 +0200


thanks to all the replies, i think the discussion can be followed throughout this message.

> You are returning an result in a function that returns void: the compiler
> will complain at you.

apologies, indeed it should have been

SEXP f(SEXP list, SEXP element);

> If you study the R Internals manual you will see that there is no space on
> the original VECSXP for another element, so you do *need* to create a new
> VECSXP. Note that creating a new list does not mean necessarily copying
> the elements, but you do need to think about the NAMED bits.

in this list i need not names associated to each element, i guess that should release me from thinking about the NAMED bits you refer to (?).

what you say about "creating a new list does not mean necessarily copying the elements" interests me. i'd like to avoid going through the old list and copy the elements. i'd like to add one at the end as fast as possible.

if, in a situation like:

l <- list(1,2)
l <- c(l,3)

R, internally, concatenates 3 to the list l avoiding to copy all 3 elements again, then i'd just like to know how R does that to do it myself as well.

> If you are doing this repeatedly you could think about exploiting the
> TRUELENGTH field to create a list with spare space that you could exploit
> in future calls.

i cannot anticipate what will be the maximum length of my list. i'm working with clique lists in undirected graphs and given a list of cliques and an edge to be removed this function will modify the clique list according to this edge removal. since i'd like R to handle graphs as big as possible with the available hardware, this list can be arbitrarily large and that's why i want to avoid going through the elements of the list.

> It is often possible to avoid copying, but considerable care is needed to
> ensure that you do not end up with an object that does not effectively
> share elements with another user-visible one.

so, would it be possible to create a new VECSXP vector with one extra element, then somehow "memcpy" the old vector of SEXP pointers into this new one, and add a brand new element with SET_VECTOR_ELT?

> > i've been looking at the R source code for everything that has to do
> > with CAR, CDR, CONS, and even found functions with promising names like
> > listAppend or GrowList but i have not been able to figure this out nor i
> > haven't been able to find any reference on how to do this by googling
> > all around, so any help will be very much appreciated.
> Those are for pairlists, not (vector) lists.


robert. mailing list Received on Fri 19 Oct 2007 - 09:12:37 GMT

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 25 Oct 2007 - 11:37:11 GMT.

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