Re: [R] correct C function usage

From: Ido M. Tamir <tamir_at_imp.univie.ac.at>
Date: Wed 14 Dec 2005 - 20:37:07 EST

On Tuesday 13 December 2005 22:35, you wrote:
> On Tue, 13 Dec 2005, Ido M. Tamir wrote:
> > Hello,
> > I am not sure if I am interfacing with C correctly and _safely_
> > or if there is a better way esp. with regards to terminating
> > the "returned" array.
>
> You need to pass the length to the C routine and check you do not
> overwrite it. (As in the parts you -snip-ed below.)

> > testFillC <- function(a){
> > .C("testFill", as.integer(a), newvalues=integer(length(a)),
> > endposition=integer(1))
> > }
>
> What do testFillC(1) or testFillC(logical(0)) do?
Thats undefined - probably a segmentation fault.

Thank you very much for your answers.

I was trying to cut down my actual function so readers could focus on what I was seeing as the main problem: terminating the array. I was hoping that somebody would tell me that I only have to terminate the array in the C function somehow and the R part would recognize that automagically - and no more passing lengths explicitly around and copying arrays up to length - e.g (ignoring wrong arguments):

void testFill(int *values, int *newvalues ){

         newvalues[0] = 1;
         newvalues[1] = 2;
         newvalues[2] =  '\0';

 }

testTestFill <- function(){
  realfilled <- testFillC( 1:10 )
  return(realfilled)
 }

testFillC <- function(a){
 .C("testFill", as.integer(a), newvalues=integer(length(a)))$newvalues  }

But I tried this and it does not help.

sincerely,
Ido Tamir



R-help@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 Received on Wed Dec 14 20:45:23 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:41:36 EST