[R] C function name garbled

Prof Brian Ripley ripley at stats.ox.ac.uk
Wed Sep 8 08:27:15 CEST 2004


On Tue, 7 Sep 2004, Sigal Blay wrote:

> On Tue, Sep 07, 2004 at 04:57:57PM -0400, Duncan Murdoch wrote:
> > On Tue, 7 Sep 2004 12:46:55 -0700, S Blay <sblay at sfu.ca> wrote :
> > 
> > >I wrote an R wrapper function (phylpro) around a C function 
> > >(Rphylpro).
> > >The first time I'm running my function, it runs with no errors.
> > >The second time I'm trying to run it, I get an error message 
> > >with the first argument to .Call garbled.
> > >
> > >Set up:
> > >> dyn.load("Phylpro.so")
> > >> source("phylpro.R")
> > >> WinHalfWidth<-30
> > >> permReps<-10
> > >> breaks<-c(548, 735, 832)
> > >
> > >First call to phylpro succeeds:
> > >> b<-phylpro("simulinfile", breaks, WinHalfWidth, permReps)
> > >>
> > >
> > >Second call to phylpro fails:
> > >> b<-phylpro("simulinfile", breaks, WinHalfWidth, permReps)
> > >Error in .Call("PSg\bBh\b", input_file = 
> > >as.character(input_file), breaks = as.integer(breaks),  : 
> > >        .Call function name not in load table
> > >
> > >Check if my C function name is in load table: 
> > >> is.loaded("Rphylpro")
> > >[1] TRUE
> > >
> > >
> > >Any ideas?
> > 
> > It looks to me as though your function is doing something to mess up
> > R's internal data.  I'd try commenting out the whole body of the
> > function, then adding it back gradually to find which part causes the
> > trouble.
> > 
> > Duncan Murdoch
> 
> I followed your advice - looks like I need some kind of a cast 
> when I assign the values of a C vector to an R vector. I think.

No, but your C is writing places it should not.

> Below is an example of something that doesn't work - 
> Can someone give me a hand? 
> (In the real function, there are also integer and Character 
> string C vectors...) 
> Thanks.
> 
> #include <R.h>
> #include <Rdefines.h>
> 
> SEXP myfunc() {
>     double *corrs; 
>     corrs[0]=3.0;

You have never assigned storage for corrs[0], so that is getting put 
somewhere random.  It is also illegal C to have an assignment in the 
middle of the declarations.  Try compiling your C with warnings, for 
example -Wall -pedantic under gcc.

>     SEXP Rcorrs; 
>     double *pRcorrs;
>     PROTECT(Rcorrs = NEW_NUMERIC(1));
>     pRcorrs = NUMERIC_POINTER(Rcorrs);
>     pRcorrs[0] = corrs[0];
> 
>     UNPROTECT(1); 
>     return(R_NilValue);
> }
> 
> /*
>  dyn.load("myfunc.so")
>  func<-function().Call("myfunc")
>  func()
> */
> 
> Run it once, it's ok:
> >  func() 
> NULL
> 
> Run it twice, not ok:
> >  func() 
> Error in .Call(NULL) : function name must be a string (of length 1)

This is not an R issue.  Time for a C course, I believe.

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




More information about the R-help mailing list