[R] How to grow an R object from C (.Call Interface)

Prof Brian Ripley ripley at stats.ox.ac.uk
Fri Oct 31 14:17:18 CET 2003


I think the solution is PROTECT_WITH_INDEX. From the Rinternals.h file

/* We sometimes need to coerce a protected value and place the new
   coerced value under protection.  For these cases PROTECT_WITH_INDEX
   saves an index of the protection location that can be used to
   replace the protected value using REPROTECT. */
typedef int PROTECT_INDEX;
#define PROTECT_WITH_INDEX(x,i) R_ProtectWithIndex(x,i)
#define REPROTECT(x,i) R_Reprotect(x,i)

You can see examples in dataentry.c, optim.c and elsewhere.

I agree that should be mentioned in R-exts.

On Fri, 31 Oct 2003, Jens Oehlschlägel wrote:

> 
> What is the best way to grow an R return object in writing a C function
> using the Rdefines.h macros.
> In my application, the final size of the return object is not known during
> construction. My understanding is that each time I grow an R object I have to
> use PROTECT() again, probably before UNPROTECTing the smaller version.
> However, due to the stack character of the PROTECT mechanism, UNPROTECT would not
> work to remove the smaller one, after the bigger has been protected. Is this
> an indication to use UNPROTECT_PTR ? Or is another approach recommended?
> 
> May be the solution to this is worth a sentence in "Wrtiting R Extensions".
> 
> Thanks for any help
> 
> 
> Jens Oehlschlägel
> 
> 

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
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




More information about the R-help mailing list