Skip to content

Problem with .C

10 messages · Grigory Alexandrovich, Jeff Newmiller, Rolf Turner +3 more

#
Hello,

I wrote a function in C, which works fine if called from the 
main-function in C.

But as soon as I try to call this function from R like .C('foo', 
as.double(x), as.integer(y)), the programm crashes.

I created a dll with the cmd command R --arch x64 CMD SHLIB foo.c and 
loaded it into R with dyn.load().

What can be the cause of such behaviour?
Again, the C-funcion itself works, but not if called from R.

Thanks
Grigory Alexandrovich
#
Without knowing that C code, we cannot know. Have you read Writing R 
Extensions carefully? I.e. take care with memory allocation and printing 
as mentioned in the manual.

Uwe Ligges
On 04.10.2011 14:04, Grigory Alexandrovich wrote:
#
On 05/10/11 01:04, Grigory Alexandrovich wrote:
It's impossible to say, with such minimal information, but a reasonable
guess is that there is a problem with the declaration of "x" and "y" in
foo.c.  These would (I think) need to be declared as double *, not double,
when foo is called from .C().

     cheers,

         Rolf Turner
#
Hi,

As other have said, it's very difficult to help you without an example
+ code to know what you are talking about.

That having been said, it seems as if you are just getting your feet
wet in this R <--> C bridge, and I'd recommend you checkout the "Rcpp"
and "inline" package to help make your life a lot easier ...

-steve

On Tue, Oct 4, 2011 at 8:04 AM, Grigory Alexandrovich
<alexandrovich at mathematik.uni-marburg.de> wrote:

  
    
1 day later
#
Hello,

first thank you for your answers.

I did not read the whole pdf Writing R Extension, but I read this 
strongly shortened introduction to this subject:

http://www.math.kit.edu/stoch/~lindner/media/.c.call%20extensions.pdf

I get the same error with this C-function:

void test(double * b, int l)
{
      int i;
      for(i=0; i < l ; i++) b[i] +=i;
}



I call it from R like this:

parameter = c(0,0,1,1,1,0,1.5,0.7,0,1.2,0.3);
.C("test", as.double(parameter), as.integer(11))

The programm crashes even in this simple case.
Where can be the error?

Thanks
Grigory Alexandrovich







Answer 1
Answer 2
Answer 3
Answer 4
#
An obvious reason might be that your second argument should be a  
pointer to int.

As others have mentioned, you might want to have a look at Rccp and/or  
inline. The documentation is good and I find it much easier to work  
with.

For example, your example could be written as:

library(Rcpp)
library(inline)

test <- cxxfunction(signature(x = "numeric" ) , '
     Rcpp::NumericVector v(x);
     Rcpp::NumericVector result(v.length());
     for (int i = 0; i < v.length(); ++i) {
         result[i] = v[i] + i;
     }
     return(result);
     ', plugin = "Rcpp" )


HTH,

Jan


Quoting Grigory Alexandrovich <alexandrovich at mathematik.uni-marburg.de>:
#
On 06.10.2011 14:51, Jan van der Laan wrote:
Oh, come on, this is now really too much of overkill.

Just make the original source


void test(double *b, int *l)
{
      int i;
      for(i=0; i < *l ; i++) b[i] += i;
}


which you would have know after reading the Wriiting R Extensions manual.

Best,
Uwe Ligges
#
Hi,

2011/10/6 Uwe Ligges <ligges at statistik.tu-dortmund.de>:
I don't agree that it's overkill -- you get to sidestep the whole `R
CMD SHLIB ...` and `dyn.load` dance this way while you experiment with
C(++) code 'live" using the inline package.

It's really handy.
I agree that this step is unavoidable no matter which avenue (Rcpp or
otherwise) one decides to take.

-steve
#
On 06.10.2011 15:41, Steve Lianoglou wrote:
You need two additional packages now where you have to rely on the fact 
those are available. Moreover, you have to get used to that syntax, and 
part of it seems to be C++ now? At least I do not know why the above 
should work at all, while I know the simple C function does.

Uwe