[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:
> Hi,
> 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[3];
>
> pv[0] = (int *) malloc(13 * sizeof(int));
> pv[1] = (int *) malloc(7 * sizeof(int));
> pv[2] = (int *) malloc(110 * sizeof(int));
>
> free(pv[1])
> ...
>
> 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[[1]] <- integer(13)
z[[2]] <- integer(7)
z[[3]] <- integer(110)
then
z[1] <- list(NULL) # and not z[[1]] <- 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
mailing list