Skip to content
Prev 23370 / 63421 Next

Passing lists from R to C, extracting elements, and sending lists back again

<snip>
This allocates memory for one k x k C array wherever C allocates temporary 
storage. You need to allocate n vectors of length k*k on the R heap to 
create a list of n matrices in R.   So you need to use allocVector(k*k, 
REALSXP) to allocate space for each element of the list, inside the loop, 
and then copy the data into that rather than into rm.

SET_VECTOR_ELT is giving a warning about incompatible pointer types, but 
the more important issue is that you need to return pointers to n 
different blocks of memory that are not going to go away after your 
function finishes.

 	-thomas
Thomas Lumley			Assoc. Professor, Biostatistics
tlumley at u.washington.edu	University of Washington, Seattle