[Rd] .C returned array has odd results

Duncan Murdoch murdoch.duncan at gmail.com
Fri Jan 27 21:29:07 CET 2012

On 27/01/2012 2:46 PM, Jonathan Lisic wrote:
> Hi, I'm sure this is a fairly simple problem, but I can't seem to find any
> specific information in the documentation as to what is going on.  I am
> writing a simple nearest neighbor program that takes in a set of of points
> on a plane and returns the neighbors of those points, for a fixed number of
> neighbors.  However, the returned integer array that is allocated in R
> seems to occasionally return incorrect results.
> I thought this would be because of GC, but I thought this was only for
> SEXPs, or at least this is the impression I got from reading the
> documentation.  So is there some obvious reason why my results seem to get
> mangled by this call?  I have checked my program through several thousand
> loops in C, and always get the same results, so I assume that this is
> something I'm doing wrong in R.

You don't show us what your incorrect results are.  The most likely 
causes are on the C side, and besides just doing the wrong calculation, 
I'd be suspicious of out of bounds writes to arrays.  Do you treat any 
of the other arguments besides the 5th one as arrays?  Are you sure the 
length of array passed in is long enough?  Do you have local allocations 
of arrays, and are they long enough?  Do your type declarations in C 
match the four double* declarations followed by six int* declarations 
implied by your .C call?

Duncan Murdoch

> Here is the code for my .C call.  I could post the C code if someone thinks
> it's important, but I'm just changing the values of integer array specified
> in R.
> r.result<-
> .C("evalPoly",
>   as.double(c.polygonX),
>   as.double(c.polygonY),
>   as.double(c.neighborX),
>   as.double(c.neighborY),
>   integer(c.numberOfNeighbors*c.height*c.length),
>   as.integer(c.polygonLength),
>   as.integer(c.neighborLength),
>   as.integer(c.numberOfNeighbors),
>   as.integer(c.length),
>   as.integer(c.height),
> )[[5]]
> Cheers,
> Jonathan
> 	[[alternative HTML version deleted]]
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

More information about the R-devel mailing list