Skip to content
Prev 15789 / 63461 Next

Using lengthgets() correctly (was SET_LENGTH)

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.