[R] array of variable length vectors

Barry Rowlingson B.Rowlingson at lancaster.ac.uk
Mon Feb 2 12:22:09 CET 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.

> 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?

  A list, with vector elements (index starts at 1 in R):

  pv = list()
  pv[[1]] = real(13)
  pv[[2]] = real(7)
  pv[[3]] = real(110)

  then the equivalent of freeing the memory and keeping the indexing 
would be:

  pv[[2]] = real(0)

  and NOT

  pv[[2]] = NULL  (which deletes element 2)

  *BUT* I dont know if R will really free() the memory at that point. 
You may need to force the garbage collection with gc()....


More information about the R-help mailing list