[Rd] Question about SET_LENGTH

Prof Brian Ripley ripley at stats.ox.ac.uk
Mon Aug 29 20:41:03 CEST 2005

SET_LENGTH is in Rdefines.h (you did not say).  It is a wrapper for

 	x = lengthgets(x, n)

and that will do what you want.  HOWEVER, you have changed x and the 
object it points to, so you do need to worry about re-protection.
So, I think that macro is rather dangerous (and it is not the only danger 
lurking in Rdefines.h, which provides no valid way to assign elements of 
character strings or lists any more).

Look and see how lengthgets is used in the R sources.

On Mon, 29 Aug 2005, James Bullard wrote:

> Hello all, thanks for all the help on the other issues. This one should
> be relatively straightforward. I have a vector of integers which I
> allocate to be the maximal size possible (meaning I'll never see more
> than cel.GetNumOutliers, but most likely I'll see less) therefore, I
> want to resize the vector, one solution would be to allocate a new
> vector then to copy into the new vector. That is what I was going to do
> until I saw the SET_LENGTH macro. Does this macro effectively take care
> of the memory? Is this an acceptable use of the macro? The code works,
> but I dont want any lurking memory problems.
> PROTECT(outliers = NEW_INTEGER(cel.GetNumOutliers()));
> if (i_readOutliers != 0) {
>     if (noutlier == 0) {
>        outliers = R_NilValue;
>      }
>      else if (noutlier < cel.GetNumOutliers()) {
>        SET_LENGTH(outliers, noutlier);
>      }
> }

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-devel mailing list