Re: [Rd] .Call question

From: Thomas Lumley <tlumley_at_u.washington.edu>
Date: Wed 26 Jul 2006 - 23:32:02 GMT

On Wed, 26 Jul 2006, Rossi, Peter E. wrote:

>
> Writing R Ext says to treat R objects that are arguments to .Call as
> read only (i.e. don't modify).
>
> I have a long list of lists that and I want to avoid the overhead of a
> copy in my C code. I would just like to modify some of the elements
> of list by replacing them with elements of exactly the same size/type.
>
> below is an example of the essence of the problem. This seems to work.
> Is this dangerous or ok? (of course, I want to do something more
> complicated
> since I can do this in R, but I hope this illustrates my question).

It is dangerous.

If you do

lst1<-list("a","b","c")
lst2<-lst1
element<-"d"
.Call("modlist",lst1, 1, element)

then both lst1 and lst2 will change. The danger is just that you can break the call-by-value illusion that R maintains, in two ways. The first is that objects that have shared structure can be modified, the second is that you can create shared structure without R knowing: if you change "element" subsequently then lst1 and lst2 may change.

         -thomas

>
> SEXP modlist(SEXP list, SEXP ind, SEXP element){
> int index;
> index=INTEGER_VALUE(ind);
> SET_ELEMENT(list,index,element);
> return(ind);
> }
>
> called in R as in
>
> lst=list("a","b","c")
> element="d"
> out=.Call("modlist",lst,1,element)
>
> here the .Call is used just for its effect on lst.
>
> thanks!
>
> peter
>
>
> ................................
> Peter E. Rossi
> Joseph T. and Bernice S. Lewis Professor of Marketing and Statistics
> Editor, Quantitative Marketing and Economics
> Rm 353, Graduate School of Business, U of Chicago
> 5807 S. Woodlawn Ave, Chicago IL 60637, USA
> Tel: (773) 702-7513 | Fax: (773) 834-2081
>
> ______________________________________________
> R-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

Thomas Lumley			Assoc. Professor, Biostatistics
tlumley@u.washington.edu	University of Washington, Seattle

______________________________________________
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Thu Jul 27 09:34:48 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Thu 27 Jul 2006 - 00:31:38 GMT.

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