[R] array of variable length vectors
Prof Brian Ripley
ripley at stats.ox.ac.uk
Mon Feb 2 12:42:32 CET 2004
On Mon, 2 Feb 2004, Giampiero Salvi wrote:
> I'd like to store N vectors of different lengths, and to be able to
> access them with an index, and eventually free the memory for one
> of them without modifying the indexes to the others.
> In C this would be a vector of N pointers that point to memory cells
> independently allocated.
> For example
> int *pv;
> pv = (int *) malloc(13 * sizeof(int));
> pv = (int *) malloc(7 * sizeof(int));
> pv = (int *) malloc(110 * sizeof(int));
> What is the best data type (or class) in R to do such a thing?
Sounds like an R list. However, in R you cannot free memory, but what
you can do (carefully) is to change the list element to NULL and then
memory will be salvaged at a future garbage collection.
z <- vector("list", 3)
z[] <- integer(13)
z[] <- integer(7)
z[] <- integer(110)
z <- list(NULL) # and not z[] <- NULL
will potentially release the memory allocated for the first element.
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595
More information about the R-help