Re: [R] segmetation fault

From: Kort, Eric <Eric.Kort_at_vai.org>
Date: Thu 29 Dec 2005 - 04:58:15 EST

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}}



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Thu Dec 29 05:07:08 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:41:42 EST