[Rd] Best Practise
Tom McCallum
termcc at googlemail.com
Mon May 14 10:23:02 CEST 2007
Hello,
Just a quick question on best practise. I am converting quite a bit of
legacy C code into R packages and have the following situation:
(1) Legacy object with a double* array in, all over code so don't want to
change any more than I have to.
(2) Do something like:
SEXP arrayToPassToR;
PROTECT( arrayToPassToR = allocVector( REALSXP, n ) );
for(i=0; i < n; i++) {
REAL(arrayToPassToR)[i] = oldCarray[i]; <---- very slow way to copy
data, can I use memcpy/pointer assignment here to remove the loop without
running into garbage collector?
}
UNPROTECT( arrayToPassToR );
SEXP returnValueFromR;
(3) Have made it to call back to an R function which returns a new /
different SEXP double array.
returnValueFromR = Test_tryEval(...);
(4) Copy back to oldCArray
for(i=0; i < n; i++) {
oldCarray[i] = REAL(returnValueFromR)[i]; <--- can I use memcpy/pointer
assignment here to remove loop?
}
UNPROTECT(1);
I have done the long winded copy as I am a bit paranoid about the garbage
collection. My question is is it legitimate to do the following
double* oldCArray = REAL(arrayToPassToR);
UNPROTECT(1); // where the 1 would remove protection from arrayToPassToR
and potential garbage collection
-- assume arrayToPassToR was garbage collected by R --
Rprintf("%f", oldCArray[0]);
because if arrayToPassToR is garbage collected then oldCArray will cause a
SEGFAULT when it is accessed afterwards, won't it?
Many thanks
Tom
--
PS Note this is the new email address - delete term at blueyonder.co.uk as it
won't work soon!
More information about the R-devel
mailing list