Skip to content

segmetation fault

4 messages · Kort, Eric, Elizabeth Lawson, Brian Ripley

#
Elizabeth Lawson Wrote:
More times than I care to remember.
written
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.
to
shuts
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}}
#
These are C programming questions, and the messages are specific to your 
OS.  Please use a more appropriate list: the posting guide does say
`questions involving C' should go elsewhere.

For the specific question here, I think you need to ask on a MacOS list.

BTW, Eric's definition is a good part of the story but not always all the 
story.  Some systems differentiate bus errors from segfaults, and some do 
not.  Generally segfaults come from SIGSEGV signals, bus errors from 
SIGBUS signals and abort traps from SIGABRT signals (and usually from 
the run-time support code calling the C function abort()).  But I get the 
feeling that is not the level of explanation you are seeking.
On Wed, 28 Dec 2005, Elizabeth Lawson wrote: