[Rcpp-devel] Persistent memory allocation in C++

schattenpflanze at arcor.de schattenpflanze at arcor.de
Mon Mar 7 09:58:32 CET 2011


Hello,

I am facing the task of writing an R interface to a rather large C++ 
library. The library does not make use of the R API. Rcpp seems to be a 
good idea, but I have the following, general questions concerning R and 
C++ integration:

1. In the C++ library, new objects are allocated on the heap in great 
numbers (using shared pointers and the new operator and using STL 
containers). The "Writing R Extensions" manual offers information about 
memory allocation in section 6.1. However, only the case of C-style 
allocations is discussed. Can I safely use "new" and do my own memory 
management? (Note that I do not use the R API, and I catch all C++ 
exceptions with the macros offered by Rcpp.)

2. In addition, I need to keep a small number of objects in memory in 
between the calls to the interface. (The construction of these objects 
is quite expensive.) More precisely, I would like to have static shared 
pointers in my C++ file which are initialized during the first call to 
one of my functions and persist until further calls (or until I detach 
my library). I have tried this already. The static shared pointer is 
initialized correctly. The object is not destructed until I manually 
reset the shared pointer, and thus free the memory, during the second 
call. I am, however, not sure whether this is _really safe_ to do (and 
whether or not it is operating system dependent).

I would very much appreciate your advice!

Best regards,
Peter


More information about the Rcpp-devel mailing list