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

Jim Java
Fri, 12 Dec 2003 13:36:55 -0000

---------- Original Message ----------------------------------
From: Prof Brian Ripley <ripley at stats.ox.ac.uk>
Date:  Fri, 12 Dec 2003 04:34:31 +0000 (GMT)

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

Thanks for your answer -- I wasn't trying to scam anything for free, just
trying to see if anyone from the Core Team could give me some advice on this
problem, which seems like something that someone would've run into before.
I've been trying to get some code done quickly (I've only been involved
in R development for about a month) and was hoping there might be a simple
answer to my problem that I'd overlooked. Since there isn't a simple
answer, I will take the time to go through the R source code to see whether I
can figure out what I must change in my code to accord with the way R does
things. It's my fault.

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

I will look at the source for lengthgets(). If mine's a protection problem,
though, I must've missed an error in my test code, because I made it as
simple as possible so as to reproduce the Access Violation, with great attention
to protecting the created SEXP's correctly. I will test the code in Linux,
too, to see whether a similar error happens.

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

If lengthgets() finally just realloc()s the vectors I've created, then what
I'm doing is quite inefficient. If you have the patience for another
question, Are R vectors represented as linked lists in C or as simple arrays?
I'm asking because I wonder how c() is implemented; I'd guess I should
call whatever c() calls within the R code to add values onto vectors, but
I'm not sure exactly what c() does there. If the answer is "You need to
study the R source yourself" that's okay; I'm new here and
don't wish to try to take more than I'm entitled to, nor do I wish to
task the beneficence of the founders!

Cheers,

Jim Java