From A.R.Runnalls at kent.ac.uk  Sun May  9 12:37:27 2010
From: A.R.Runnalls at kent.ac.uk (Andrew Runnalls)
Date: Sun, 09 May 2010 11:37:27 +0100
Subject: [Rd] Memory allocation in C/C++ vs R?
In-Reply-To: <u2vd4cf43b61004301040jc5a99578k9b44b41e7b414b0a@mail.gmail.com>
References: <m2qd4cf43b61004292203x8265d834wb1d42c483813d55d@mail.gmail.com>	<491D8740-466D-4442-B6BE-942A9C423CD0@r-project.org>
	<u2vd4cf43b61004301040jc5a99578k9b44b41e7b414b0a@mail.gmail.com>
Message-ID: <4BE69067.10205@kent.ac.uk>

Dominick,

On 30/04/10 18:40, Dominick Samperi wrote:
> 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?

If you had time to try out your work with CXXR, I would be very
interested to hear your experiences.  Picking up on some of the points
already raised in this thread:

1. CXXR's main program is written in C++, so it should deal with C++
initialisation and teardown correctly.  (However, it has been so far
tested only with gcc and the Intel C++ compiler, so there are doubtless
plenty of submerged rocks still to discover.)

2. Within CXXR, all indirect flows of control are handled using C++
exceptions, and it is certainly the intention that user-supplied code
should also be free to use C++ exceptions (subject to the caution that
is always necessary in using C++ exceptions).

3. As regards memory allocation, in addition to the mechanisms available
in standard R, you can of course use standard C++ 'new' and 'delete'.
Also, objects of any C++ class that inherits directly or indirectly from
CXXR::GCNode will be looked after by CXXR's own internal garbage collection.

4. Regarding protection from garbage collection, the mechanisms of
standard R are available.  But for C++ code, the preferred approach is
to use the CXXR::GCRoot<T> and CXXR::GCStackRoot<T> smart pointers.
Basically an object of one of these types behaves like a plain T*
pointer (where T inherits from CXXR::GCNode), but whatever it points to
is protected from garbage collection - for as long as the pointer exists
and points to that object.

Let me know (offline from this list) if I can be of any help.

Andrew Runnalls



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