[Rd] .Call question

Thomas Lumley tlumley at u.washington.edu
Thu Jul 27 01:32:02 CEST 2006


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 at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

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



More information about the R-devel mailing list