Hi all,

I am using c++ functions in R by Rcpp and inline packages. 
The code is quite simple, but the R session always automatically crash after some running time.
Does anyone here familiar with Rcpp and inline? What’s the problem in the following code?
I have checked the input values, no NA and other strange value exists.
Thank you for your attention!


> mkc <- cxxfunction( signature(fx0="vector",fy0="vector",fsp0="vector",
                x0="vector",y0="vector",sp0="vector",phyd0="matrix",
                rmax0="numeric",step0="numeric",binlength0="integer"),
        paste(readLines("mkc.cpp"),collapse = "\n"),
        plugin = "Rcpp")

The codes in mkc.cpp file are:

NumericVector fx(fx0);
NumericVector fy(fy0);
NumericVector fsp(fsp0);
NumericVector x(x0);
NumericVector y(y0);
NumericVector sp(sp0);
NumericMatrix phyd(phyd0);
double rmax = as<double>(rmax0);
double step = as<double>(step0);
double  totalcout = 0.0;
double  totalsum = 0.0;

int binlength = as<int> (binlength0);

NumericVector bincout(binlength);
NumericVector binsum(binlength);

int nfocal=fx.size();
int ntotal=x.size();
double dist =0.0;
double ibin = 0.0;
double lpd = 0.0;

typedef NumericVector::iterator vec_iterator;
vec_iterator ifx = fx.begin(), ify = fy.begin();
vec_iterator ix= x.begin(), iy = y.begin();
vec_iterator ifsp = fsp.begin(), isp = sp.begin();


for (int i = 0; i < nfocal; i++){
    for (int j= 0; j < ntotal; j++){
        dist=pow(pow(ix[j]-ifx[i],2)+pow(iy[j]-ify[i],2),0.5);

        if(dist == 0.0 || ifsp[i]==isp[j] || dist > rmax ){
            continue;
        }

        ibin=dist/step;
        bincout[ceil(ibin)]++;
        bincout[floor(ibin)]++;
        totalcout++;

        lpd=phyd(ifsp[i],isp[j]);
        binsum[ceil(ibin)]+=lpd;
        binsum[floor(ibin)]+=lpd;
        totalsum+=lpd;
    }
}

double k = totalsum/totalcout;
GenericVector result(4);
result[0]=totalcout;
result[1]=totalsum;
result[2]=bincout;
result[3]=binsum;

delete ifx;
delete ify;
delete ix;
delete iy;
delete ifsp;
delete isp;

return result;


Best wishes!
Guochun


	[[alternative HTML version deleted]]

