[R] segmetation fault

Kort, Eric Eric.Kort at vai.org
Wed Dec 28 18:58:15 CET 2005


Elizabeth Lawson Wrote:
> 
> Hey,
> 
>   I don;t know if anyone has come across this error before...

More times than I care to remember.

> 
>   I am running R on the terminal of my MAC OS X 10.3.4 and I have
written
> C code and compiled it using
>   R CMD SHLIB mycode.c
>   There were no problems in compiling so I now have mycode.o and
mycode.so.

A segmentation fault occurs when you try to access memory you didn't
allocate for your use (see below).  In other words, you are trying to
use memory outside the segment allocated to your program (which, if
allowed, could result in corrupting memory being used by other
programs--which brings back unhappy memories of days gone by when an
error in one program could crash the whole system). So these kinds of
problems will not show up at compile time...only when you actually run
the program.

>   I used dyn.load("mycode.so") and again, no problems.  But when I try
to
> use the code .C("mycode",x) I get the error Segmentation fault and R
shuts
> down.  Any ideas as to where my problem could be?

When I run into a segmentation fault, it is usually because I am trying
to access an element of an array that is beyond what I have allocated,
as in...

int main()
{
  int *a;
  a = (int*) malloc(3*sizeof(int));
  printf("Fasten your seatbelts...\n");
  a[4000] = 12;
  return(0);
}

One less obvious way this can happen is forgetting to initialize your
arrays to the proper length before passing a reference to them to your C
function.  

If you still are having trouble, you could post a small snippet of code
that recreates the error for us to examine.

HTH,
Eric


This email message, including any attachments, is for the so...{{dropped}}




More information about the R-help mailing list