[Rcpp-devel] Rcpp-devel] What is the best practice to expose a C structure from 3rd party library into R?
edd at debian.org
Wed Jul 24 04:18:43 CEST 2013
On 23 July 2013 at 14:46, Steve Jaffe wrote:
| I agree that STL containers should be used whenever possible and new/delete avoided whenever possible.
| However, there are times when explicit 'new' cannot be avoided (I'm now referring only to C++.) In those cases the best way to avoid the need for explicit memory management is to use an appropriate "smart pointer" such as boost::shared_ptr (std::shared_ptr as of C++11). Then one never has to explicitly call 'delete.'
And sometime you need some manual allocation (we do have that in a few places
inside the Rcpp code -- but it also deals with a C API). But we agree: less
and less often, and ideally as little as possible.
| To clarify, though: what I said originally about NULL pointers applies to both C (malloc/free) and C++ (new/delete).
Right, I agree and I once knew that too. I guess my habit just became to
assign NULL in order to have a 'flagged' value that can be tested for. And
yes, these days compilers are better and more forgiving. Still no excuse to
use pointers :)
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com
More information about the Rcpp-devel