[Rd] Memory allocation in C/C++ vs R?

Simon Urbanek simon.urbanek at r-project.org
Fri Apr 30 15:00:24 CEST 2010


Brian's answer was pretty exhaustive - just one more note that is indirectly related to memory management: C++ exception handling does interfere with R's error handling (and vice versa) so in general STL is very dangerous and best avoided in R. In addition, remember that regular local object rules are broken because you are not guaranteed to leave a function the regular way so there is a high danger of leaks and inconsistencies when using C++ memory management unless you specifically account for that. That said, I have written C++ code that works in R but you have to be very, very careful and think twice about using any complex C++ libraries since they are unlikely written in R-safe way.

Cheers,
Simon


On Apr 30, 2010, at 1:03 AM, Dominick Samperi wrote:

> The R docs say that there are two methods that the C programmer can
> allocate memory, one where R automatically frees the memory on
> return from .C/.Call, and the other where the user takes responsibility
> for freeing the storage. Both methods involve using R-provided
> functions.
> 
> What happens when the user uses the standard "new" allocator?
> What about when a C++ application uses STL and that library
> allocates memory? In both of these cases the R-provided functions
> are not used (to my knowledge), yet I have not seen any problems.
> 
> How is the memory that R manages and garbage collects kept
> separate from the memory that is allocated on the C++ side
> quite independently of what R is doing?
> 
> Thanks,
> Dominick
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
> 
> 



More information about the R-devel mailing list