[R] allocating memory in C, not in R
Sundar Dorai-Raj
sundar.dorai-raj at PDF.COM
Thu Sep 2 03:11:37 CEST 2004
Sigal Blay wrote:
> Thank you for the fast reply.
>
> Below is a simplified version of my c function that I am
> currenctly using with a .C() call.
> The values that has to be returned to R are the three outVectors.
> If I need to convert .C to .Call,
> How do I do it based on myFunction below?
>
> Thank you for your help.
>
>
> void myFunction(char **argv, int *inputVector,
> int *RoutVector1, double *RoutVector2, char **RoutVector3) {
> int *outVector1;
> double *outVector2;
> char **outVector3;
> int roof = 0;
> roof = calculate_values ( argv[0],
> inputVector,
> &outVector1,
> &outVector2,
> &outVector3 )
>
> for(i=0;i<roof;i++) {
> RoutVector1[i] = outVector1[i];
> RoutVector2[i] = outVector2[i];
> }
> return;
> }
>
>
Well, you could read "Writing R Extensions" (Section 4.8). Your function
will have to change significantly, though. I would create a list within
the C-function (called "val" below) and populate the RoutVectors with it.
(I can't remember whether you'll need more than #include <R.h>. Again, I
direct you to the documentation.)
SEXP myFunction(SEXP argv, SEXP inputVector) {
SEXP val;
PROTECT(val = allocVector(VECSXP, 3));
/* some code you'll have to write */
UNPROTECT(1);
return val;
}
# in R
RoutVectors <- .Call("myFunction", argv, inputVector)
--sundar
> On Wed, Sep 01, 2004 at 10:16:59PM +0100, Prof Brian Ripley wrote:
>
>>On Wed, 1 Sep 2004, S. Blay wrote:
>>
>>
>>>I need to retrieve several vectors of various types from a call
>>>to .C(), but I don't know their length in advance.
>>>Is there a way to do this without allocating an excessive amount
>>>of memory?
>>>If so, an example would be very helpful.
>>
>>It would be very much easier to use .Call rather than .C.
>>
>>Alternatively, generate and allocate in C on one C call and retrieve on a
>>second, as rpart does.
>>
>>--
>>Brian D. Ripley, ripley at stats.ox.ac.uk
>>Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
>>University of Oxford, Tel: +44 1865 272861 (self)
>>1 South Parks Road, +44 1865 272866 (PA)
>>Oxford OX1 3TG, UK Fax: +44 1865 272595
>
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
More information about the R-help
mailing list