Re: [R] array of variable length vectors

2004-02-02 Thread Barry Rowlingson
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()

Baz

__
[EMAIL PROTECTED] mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] array of variable length vectors

2004-02-02 Thread Prof Brian Ripley
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,  [EMAIL PROTECTED]
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, UKFax:  +44 1865 272595

__
[EMAIL PROTECTED] mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] array of variable length vectors

2004-02-02 Thread Uwe Ligges
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?
See ?list

Uwe Ligges

__
[EMAIL PROTECTED] mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html