Re: [Rd] .C returned array has odd results

From: Duncan Murdoch <>
Date: Fri, 27 Jan 2012 15:29:07 -0500

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]]
> ______________________________________________
> mailing list
> mailing list Received on Fri 27 Jan 2012 - 20:33:15 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 27 Jan 2012 - 21:20:12 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive