[R] allocating memory in C, not in R

Sigal Blay sblay at sfu.ca
Fri Sep 3 00:30:04 CEST 2004


> >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

Thanks Sundar - but how do I return several vector objects of 
different modes wrapped in a list object using .Call?

Sigal




More information about the R-help mailing list