[Rd] Best Practise

From: Tom McCallum <termcc_at_googlemail.com>
Date: Mon, 14 May 2007 09:23:02 +0100


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?

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


PS Note this is the new email address - delete term_at_blueyonder.co.uk as it  
won't work soon!

R-devel_at_r-project.org mailing list
Received on Mon 14 May 2007 - 08:31:56 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 14 May 2007 - 23:34:15 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.