Segmentation Fault Article Index for
Segmentation
Website Links For
Segmentation
 

Information About

Segmentation Fault




Segmentation is one approach to Memory Management and protection in the Operating System . It has been superseded by Paging for most purposes, but much of the terminology of segmentation is still used, "segmentation fault" being an example. Some operating systems still have segmentation at some logical level although paging is used as the main memory management policy.

On Unix-like operating systems, a process that accesses invalid memory receives the SIGSEGV Signal . On Microsoft Windows , a process that accesses invalid memory receives the STATUS_ACCESS_VIOLATION Exception .


EXAMPLE

Here is an example of ANSI C code that should create a segmentation fault on platforms with memory protection:


  • s = "hello world";

  • s = 'H';



When the program containing this code is Compiled , the String "hello world" is placed in the section of the program Binary marked as read-only; when loaded, the operating system places it with other strings and Constant data in a read-only segment of memory. When executed, a variable, ''s'', is set to point to the string's location, and an attempt is made to write an ''H'' character through the variable into the memory, causing a segmentation fault. Compiling and running such a program on OpenBSD 4.0 produces the following Runtime Error :


$ gcc segfault.c -g -o segfault
$ ./segfault
Segmentation fault


Backtrace from Gdb :


Program received signal SIGSEGV, Segmentation fault.
0x1c0005c2 in main () at segfault.c:6
  • s = 'H';



In contrast, gcc 4.1.1 on Linux produces a Compile-time Error by default:


$ gcc segfault.c -g -o segfault
segfault.c: In function ‘main’:
segfault.c:4: error: assignment of read-only location


The conditions under which segmentation violations occur and how they manifest themselves are specific to an operating system.

Because a very common program error is a Null Pointer dereference (a read or write through the null pointer, a pointer to address 0, commonly used in C to mean "pointer to no object" or as an error indicator), most operating systems map the first page of memory (starting at address 0) so that accesses to it cause a segmentation fault.


SEE ALSO



EXTERNAL LINKS