[R] C++: Appending Values onto an R-Vector.

Prof Brian Ripley
Fri, 12 Dec 2003 04:34:31 -0000

This was posted so it was not lost.  Probably no one felt like giving you
free technical support.

You are using the Rdefines.h macros with which few of use are familiar.
But you will find the native version, lengthgets, used to extend vectors
in several places in the R sources.  I don't understand your code (the
real code is hidden behind an obscuring layer of macros), but this sounds
very like a protection problem.

In any case, lengthgets just reallocates and copies and it probably more
efficient to do that yourself.

On Fri, 12 Dec 2003, Jim Java wrote:

> Hi folks. I posted this question a few days ago, but maybe it got lost
> because of the code I included with it. I'm having a problem using the
> SET_LENGTH() macro in an R extension I'm writing in C++. In a function
> within the extension I use SET_LENGTH() to resize R vectors so as to
> allow the concatenation of single values onto the vectors -- it's a
> "push back" function to append values onto the end of a vector.
> However, when I use this function to push back a large number of
> values one at a time, Rgui.exe (I'm working with R 1.8.1 in Windows
> XP) crashes from an Access Violation; if, however, I pre-allocate
> space (is the space actually pre-allocated?) for the vector (say with
> NEW_INTEGER(n) rather than NEW_INTEGER(0)) and insert values into the
> allocated slots, the code works fine. If you'd like to see some test
> code, I've already posted it here:
>
> https://www.stat.math.ethz.ch/pipermail/r-help/2003-December/041871.html
>
> Here's my question, then: Is SET_LENGTH() the appropriate way to
> create space for tacking values onto the end of an R-vector in C++, or
> should I be trying to tack them on in some other way?

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