From simon.urbanek at r-project.org  Sun Apr 19 17:00:57 2009
From: simon.urbanek at r-project.org (Simon Urbanek)
Date: Sun, 19 Apr 2009 11:00:57 -0400
Subject: [Rd] export C++ array to R
In-Reply-To: <23110405.post@talk.nabble.com>
References: <23110405.post@talk.nabble.com>
Message-ID: <8E0F99E8-B8CB-4550-AA4A-035355A1DF86@r-project.org>


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



[Rd] export C++ array to R
Simon Urbanek