Segmentation Faults

Paul Richards paul at originative.co.uk
Fri Nov 10 13:26:14 GMT 2000


James Diss wrote:
> 
> In short;
> 
> What are they?  Are they an indicator of iffy hardware or are they just
> unavoidable?

In an operating system like FreeBSD the kernel prevents programs from
accessing each other's memory. When a program attempts to do so the
kernel will send it a segmentation fault and unless the program has
trapped these it will core dump. This is all part of the protection that
Unix provides that makes it safe to run lots of programs on the same
box.

Generally though a segmentation fault is caused by the common
programming error of trying to access data through a pointer when the
pointer is null. This is in fact such a common programming error that
the kernel maps page 0 as being unavailable to anyone, so any program
that tries to access data in page zero, which is where a null pointer is
pointing will get a segmentation fault.

There's no technical reason why page 0 can't be used by applications,
the memory locations 0-PAGESIZE-1 are perfectly valid locations but
since it's so easy when writing C code to get pointer initialisation
screwed it helps to catch the basic bugs very quickly, so to prevent
location 0 being a valid address the kernel makes page 0 unavailable and
issues a segmentation fault to any program that tries to access it.

So, by far by a long long way the reason for a segmentation fault is a
programming error.

Paul.




More information about the Ukfreebsd mailing list