Skip to content
Prev 309459 / 398506 Next

R crashing after successfully running compiled code

On 12-10-30 11:13 PM, Adam Clark wrote:
I think your C code has a bug in it.  The bug might go away when you 
rewrite the function to work within the .Call convention, but it is 
probably easier to find the bug and fix it with the current code.

Things to look for:

Are you fully allocating all arrays in R before passing them to C?  The 
C code receives a pointer and will happily write to it, whether that 
makes sense or not.

Are you careful with your limits on vectors?  In R, a vector is indexed 
from 1 to n, but the same vector in C is indexed from 0 to n-1.  If the 
C code writes to entry n, that will eventually cause problems.

Are you allocating memory in your C code?  There are several ways to do 
that, depending on how you want it managed.  If you do it one way and 
expect it to be managed in a different way, you'll get problems.

Duncan Murdoch