[R] Calling C funtion from R help Needed

Steve Lianoglou mailinglist.honeypot at gmail.com
Mon Aug 31 22:41:53 CEST 2009


Hi,

Perhaps you should try just passing in two integers to some toy C  
function just to make sure all the plumbing is correct before you play  
with strings/char arrays.

In any case, some comments inline:

On Aug 31, 2009, at 11:42 AM, naresh kumar wrote:

> Hello Forum,
>
>  I'm calling C function from R.It is a
>          small sample trial
>  program. The C function will accept a string and a integer
>  and print them.
>  It is giving error segmentation fault.
>          Below are the C function, Wrapper code ,R code and
>  R
>          output.
>  Please help me in this issue
>  Thank you in
>          advance
>
> Error:
>
>  *** caught segfault ***
> address 0x68, cause 'memory not mapped'
>
> Traceback:
>  1: .C("checkstr_R_wrapper", n = as.character(n), m =  
> as.integer(m),     NAOK = TRUE, DUP = TRUE, result = integer(1))
>  2: checkstr("hi", 2)
>
> Possible actions:
> 1: abort (with core dump, if enabled)
> 2: normal R exit
> 3: exit R without saving workspace
> 4: exit R saving workspace
> Selection: 1
> aborting ...
> Segmentation fault
>
>
>
> C FUNCTION (Name : checkstr.c)
> #include<stdio.h>
> #include<string.h>
>  int n_char(char n[10],int m)
> {
>    int result;
>    result = 3;
>    result = strlen(n);
>    printf("the string is %s\n",n);
>    printf("the silly number is %d\n",m);
>   return result;
>
> }

Why are you defining the first argument in this function as ``char  
n[10]``. How do you know it's 10 chars long? My C is a bit rusty, but  
I'm guessing this might be causing a problem (like a seg fault!) if  
your n isn't 10 chars long.

> WRAPPER CODE
>          (wrapcheckstr.c)
> #include <stdio.h>
> #include<string.h>
>
> int n_char(char,int);
> void checkstr_R_wrapper(char **n,int *m, int *result)
> { *result = n_char(**n,*m); }
> [proddun at venus myrpackages]$
>
> R CODE(checkstr.R)
>
> source('checkstr.R')
> checkstr <- function(n,m) {
> if (!is.loaded(symbol.C('checkstr_R_wrapper')))  
> { dyn.load('checkstr.so') }
>  returned_data = .C('checkstr_R_wrapper',  
> n=as.character(n),m=as.integer(m), result=integer(1))
> return(returned_data$result) }

If you're going to be passing in arrays of anything (chars, ints,  
etc), you should also pass in a sister variable that is the length of  
the array itself.

So, your two functions might be:

void checkstr_R_wrapper(char **n, int n_length, int *m, int *result) {
   *result = n_char(**n,*m);
}

and the R call would be:

.C('checkstr_R_wrapper',
     n=as.character(n),
     n_length=length(as.character(n)),
     m=as.integer(m), result=integer(1))

... or something ... perhaps.

Does that help any?

-steve

--
Steve Lianoglou
Graduate Student: Computational Systems Biology
   |  Memorial Sloan-Kettering Cancer Center
   |  Weill Medical College of Cornell University
Contact Info: http://cbio.mskcc.org/~lianos/contact




More information about the R-help mailing list