From djsamperi at gmail.com  Fri Apr 30 19:40:12 2010
From: djsamperi at gmail.com (Dominick Samperi)
Date: Fri, 30 Apr 2010 13:40:12 -0400
Subject: [Rd] Memory allocation in C/C++ vs R?
In-Reply-To: <491D8740-466D-4442-B6BE-942A9C423CD0@r-project.org>
References: <m2qd4cf43b61004292203x8265d834wb1d42c483813d55d@mail.gmail.com>
	<491D8740-466D-4442-B6BE-942A9C423CD0@r-project.org>
Message-ID: <u2vd4cf43b61004301040jc5a99578k9b44b41e7b414b0a@mail.gmail.com>

Simon,

Just to be sure that I understand, are you suggesting that the R-safe way to do
things is to not use STL, and to not use C++ memory management and
exception handling? How can you leave a function in an irregular way without
triggering a seg fault or something like that, in which case there is no chance
for recovery anyway?

In my experience the C++ exception stack seems to
unwind properly before returning to R when there is an exception, and memory
that is allocated by C++ functions seems to maintain its integrity and does
not interfere with R's memory management.

It would be helpful if you could specify what kind of interference you
are referring to here between C++ exception handling and R's error
handling, and why STL is dangerous and best avoided in R. I have
used STL with R for a long time and have experienced no problems.

The fact that R has a C main may be problematic because C++ static
initializers may not be called properly, but the fact that packages are
usually loaded dynamically complicates this picture. The dynamic
library itself may take care of calling the static initializers (I'm not
sure about this, and this is probably OS-dependent). One possible
work-around would be to compile the first few lines (a stub) of
R main using the C++ compiler, leaving everything else as is
and compiled using the C compiler (at least until CXXR is widely
available).

Since C++ (and STL) are very popular it would be helpful for developers
to have a better idea of the benefits and risks of using these tools
with R.

Thanks,
Dominick

On Fri, Apr 30, 2010 at 9:00 AM, Simon Urbanek
<simon.urbanek at r-project.org> wrote:
> 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
>>
>>
>
>



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