[Rd] Using lengthgets() correctly (was SET_LENGTH)

Prof Brian Ripley ripley at stats.ox.ac.uk
Sun Apr 17 09:10:36 CEST 2005


If you use R not the S-compatibility macros you will be less likely to 
confuse yourself.  The definition is (analogous to realloc)

Rdefines.h:#define SET_LENGTH(x, n)  (x = lengthgets(x, n))

so x is changed and needs to be reprotected.  [I don't know that
SET_LENGTH *is* part of the API: it is not defined in `Writing R 
Extensions'.]

Your error will potentially occur at the first garbage collection.  Try 
using REPROTECT.

Note though that what you are doing is very inefficient, and re-setting 
the length without changing the dim is also an error.  What R's internal 
examples do is to allocate a modest vector, double in size when needed, 
call lengthgets to shorten at the end, then set the dim attribute.

On Sat, 16 Apr 2005, Larry Tordsen wrote:

> Hello.
>
> I'm trying to read a delimited file.  I'm a little new
> to the R api.  In the program, I do something like
> this...
>
> /*****************************/
> SEXP retval;
> PROTECT(retval = allocMatrix(STRSXP,1,names.dim));
> int r_eltctr = 0;
> while(!myfile.eof())
> {
>  SET_LENGTH(retval,r_eltctr+num_of_fields);
>  for(int i = 0;numofcolumns; ++i)
>    {
>      INTEGER(retval)[r_eltctr++] = 1;/*some
> value...*/
>    }
>  ++r_eltctr;
> }
> UNPROTECT(1);
> /***************************************/
>
> There is a segmentation fault when SET_LENGTH() is
> called several iterations into the program.  It seems
> like it should not be a memory issue, unless a
> constraint is set by R.  The vector only has about
> 3500 elements at the time it crashes.
>
> Does anyone have any ideas what the problem could be?

The problem seems to be the assumption that R (and not the user) was at 
fault.

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