[Rd] export C++ array to R
Simon Urbanek
simon.urbanek at r-project.org
Sun Apr 19 17:00:57 CEST 2009
On Apr 18, 2009, at 4:12 AM, whizvast wrote:
>
> Hi, I am a newbie on C++
>
> Right now I have an array of doubles in C++.
>
> Is there a way to "export" that array into R? Of course, I can
> allocate
> the memory block first using "allocVector" and copying the array
> contents
> one by one.
>
> But, what if that array is fairly large? Copying doesn't look that
> efficient.
> I was thinking of setting the data pointer(DATAPTR) point to that
> array,
> and adjust the LENGTH of SEXP. But I don't know how to do that.
>
The short answer is no, you can't, because the memory has to be
allocated by R. If you are allocating the memory yourself, you can
simply use allocVector instead of malloc/new to allocate the array in
the first place - that saves you the copying and is the more usual
approach for such R packages.
Cheers,
Simon
More information about the R-devel
mailing list