[R] using the .C interface to call compiled C code
Faheem Mitha
faheem at email.unc.edu
Fri Feb 23 07:35:11 CET 2001
Dear People,
I have been trying to learn how to use the .C interface. A friend gave me
a toy example, and I have been playing with it. It is
(C code)
#ifdef USING_R
typedef int Sint;
#else
typedef long Sint;
#endif
void kosum(double *a, double *b, Sint *na)
{
int i, j, nab;
double temp;
nab=*na;
temp=0.0;
for(i=0;i < nab; i++)
{
temp=temp+a[i];
}
*b=temp;
}
And the calling code in R is
test1<-
function(a, b)
{
.C("kosum",
as.double(a),
b = double(1),
as.integer(length(a)))[[2]]
}
Now, I noticed that if I change
for(i=0;i < nab; i++) to say for(i=0;i <= nab; i++) or even
for(i=0;i <= nab + 1; i++)
everything seems happy, and I get the same answers. But I don't understand
why this is so. I am surely trying to read from nonexistent memory in this
case, and I should have some problems.
I may be missing the obvious, but I am neither very experienced in R or in
C.
Also, if anyone has a simple example of using .C that they would like to
share with me, or if they can point to me to (very) simple examples in the
source code that would be suitable for an beginner, I would be extremely
grateful.
Sincerely, Faheem Mitha.
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list