[Rcpp-devel] Best way to return raw array

Davor Cubranic cubranic at stat.ubc.ca
Fri Sep 2 19:26:37 CEST 2011


On 2011-09-02, at 5:19 AM, Dirk Eddelbuettel wrote:

> Maybe this would do
> 
>  double *p=third_party_function(nn);
>  NumericVector ret(nn);           // new memory
>  copy(ret.begin(), ret.end(), p); // untested
>  delete p;
>  return ret;
> 
> | where third_party_function() is C legacy code that is documented as
> | returning a block of memory of size nn that the client should take
> | ownership of.

If third_party_function is really C legacy code, wouldn't the block of code be more likely to have been allocated with a 'malloc', and so you should release it with a 'free'? 

http://stackoverflow.com/questions/2570223/malloc-and-delete-in-c-opinions

Davor



More information about the Rcpp-devel mailing list