[R] Call and memory

Thomas Lumley tlumley at u.washington.edu
Sat Jan 10 00:45:55 CET 2004


On Fri, 9 Jan 2004, Bob Wheeler wrote:

> I use a large real matrix, X, in C code that is passed from R and
> transposed in place in the C code. I would like to conserve memory and,
> if possible, allocate space for only one copy of X -- hence I would like
> to pass a pointer to the data in the X object to the C code.
>
> The Writing R Extensions manual says that neither .Call nor .External
> copy their arguments. They also say that these arguments should be
> treated as read only.
>
> Fine, but in testing I seem to be able to transpose very large X's in
> place, in C code without an error. This leads me to assume that the
> manual was just giving good advice about treating arguments as read
> only. However, I find that I have done nothing to the X in R. It seems
> that a copy has been made after all. I may as well call the code with .C
>   and avoid the use of macros.
>
> Could someone please point out the error in my thinking or suggest a way
> to accomplish my goal?
>
> My code follows:
>
> "mListTest" <-
> function(X,N,k) {
>
> 	.Call("mList",as.double(X),as.integer(N),as.integer(k));
> }
>


as.double(X) returns a copy of X, so you are only passing a copy to C.


	-thomas




More information about the R-help mailing list