Skip to content

SET_LENGTH

3 messages · Larry Tordsen, Brian Ripley

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

            
The problem seems to be the assumption that R (and not the user) was at 
fault.
#
I assume this is not true of your actual example, but the sample code
allocates a character matrix and calls INTEGER() on it.  Ouch!
On Sun, 17 Apr 2005, Prof Brian Ripley wrote: