On Mon, 1 Mar 2004, Vadim Ogranovich wrote: > Could someone please point to an example of passing strings from .C() > calls back to R?
There are not many, as such things are better done using .Call. > I want to be able to do something like this: > > str <- .C("return_foo_string", str=character(1))$str > > void return_foo_string(char ** str) { > *str = "foo"; > } > > The above code has at least two memory allocation "concerns": > 1) How to properly allocate "foo". I should probably use R_alloc, e.g. > > char foo[] = "foo"; > *str = R_alloc(sizeof(foo), 1); That would suffice. Most uses I see in R addons either use a static buffer or preallocate enough space in the character vector passed in to .C (e.g. function tghyper in package SuppDists). > 2) I don't know if the string pointed to by *str before the > re-assignment, which now becomes dangling, will be properly reclaimed. You need to consult the source code (here src/main/dotcode.c). The R character vector is copied to a **char construct on the way in, and the **char construct is copied back to an R character vector on the way back. All the allocation is done by R_alloc, and its stack is reset as do_dotcode is left. So storage is reclaimed at the next garbage collection after the .C call. -- Brian D. Ripley, [EMAIL PROTECTED] Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595 ______________________________________________ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html