Skip to content
Prev 36630 / 63424 Next

Memory allocation in C/C++ vs R?

On Sat, May 1, 2010 at 5:02 AM, Romain Francois
<romain.francois at dbmail.com> wrote:
The manual is also missing information on R_PreserveObject() and
R_ReleaseObject(), useful but undocumented features that are
exploited in Rcpp to elininate the need for the ubiquitous
PROTECT/UNPROTECT macros.
Good idea.
Nice trick.

Given the use of undocumented features and the hacks, wouldn't it make
more sense to make Rcpp part of the R core, a kind of limited implementation
of CXXR?

With these hacks, along with a small change in the way R is built, it seems
like most of the C++ issues disappear. The change I am referring to is to
compile main.c (or a stub part of it) using C++ instead of C.

This would take care of the one remaining C++ issue: static initializers not
being called. Apparently this issue does not come up in most situations
because R links to packages that use C++ via the runtime system /
shared library mechanism, and this mechanism takes care of the
necessary static initialization. This may not work under all OS's
though, and building main.c using C++ would solve this problem.

Another advatage of building main.c using C++ is that R becomes
a C++ application (that just happens to be largely composed of C
modules). This would open more possibilities for R development
without requiring a complete refactoring as in the case of CXXR.

Dominick