On Wed, 26 Jan 2000 msvika@mscc.huji.ac.il wrote:
>
> I am trying to call a C compiled code to S. It seems to me that I do all
> possible by recommendations of several guides (including Modern Applied
> Statistics with S+) but even a very simple example don't run in S.
> For ex, I write in C to calculate exp(-x*x/2)
> and after "successful" compilation I receive in S:system terminating.bad
> address. It's strange for me that the same code that calculates exp(x)
> runs well!!!
> Where is my mistake?
> I'll be glad if somebody have a simple set of training examples with
> explanations.
Well, MASS's on-line complements and the S-PLUS manuals do. It is
impossible to tell you what you did wrong without seeing your example or
even knowing your platform or version of S-PLUS, but let me guess that you
did not ensure that all the arguments to your C function were pointers or
you manipulated the pointers incorrectly. To do your example
exp2.c:
#include <math.h>
void exp2(double *x)
{
*x = exp(-0.5 * (*x) * (*x));
}
Switch on all your compiler's error checking to track down any type errors.
Compile and load this, then use it by
exp2 <- function(x) .C("exp2", ans=as.double(x))$ans
works.
--
Brian D. Ripley, ripley@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 272860 (secr)
Oxford OX1 3TG, UK Fax: +44 1865 272595
-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu. To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message: unsubscribe s-news
|