Segmentation fault


Segmentation fault

A segmentation fault (often shortened to segfault) is a particular error condition that can occur during the operation of computer software. A segmentation fault occurs when a program attempts to access a memory location that it is not allowed to access, or attempts to access a memory location in a way that is not allowed (for example, attempting to write to a read-only location, or to overwrite part of the operating system). Systems based on processors like the Motorola 68000 tend to refer to these events as "address" or "bus" errors.

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, and usually a window asking the user to send an error report to Microsoft appears.

Example

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

char *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 such a program with a compiler that does not check for the assignment of read-only locations at compile time, and running it on a Unix-like operating system 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 6 *s = 'H';

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 null pointer, used in C to mean "pointer to no object" and as an error indicator), most operating systems map the null pointer's address such that accessing it causes a segmentation fault.

int* ptr = (int*)0; *ptr = 1;

This sample code creates a null pointer, and tries to assign a value to its non-existent target. Doing so causes a segmentation fault at runtime on many compilers.

Another way to cause a segmentation fault is to recurse without a base case, which causes a stack overflow:

int main(){ main();}

See also

*Buffer overflow
*Bus error
*Core dump
*General protection fault
*Segfault, a humor website named after this error.

External links

* [http://www.faqs.org/qa/qa-673.html]
* [http://c-faq.com/null/null1.html]
* [http://c-faq.com/null/varieties.html]


Wikimedia Foundation. 2010.

Look at other dictionaries:

  • Segmentation Fault — Bei Computern tritt eine Schutzverletzung (v. engl. segmentation fault oder segmentation violation, kurz auch segfault) auf, wenn ein Computerprogramm auf eine Ressource (insbesondere auf Speicher) zuzugreifen versucht, die vor einem solchen… …   Deutsch Wikipedia

  • Segmentation Fault — Erreur de segmentation Pour les articles homonymes, voir Segmentation. Une erreur de segmentation (en anglais segmentation fault, parfois appelé en abrégé segfault), est un plantage d une application qui a tenté d accéder à un emplacement mémoire …   Wikipédia en Français

  • Segmentation fault — Erreur de segmentation Pour les articles homonymes, voir Segmentation. Une erreur de segmentation (en anglais segmentation fault, parfois appelé en abrégé segfault), est un plantage d une application qui a tenté d accéder à un emplacement mémoire …   Wikipédia en Français

  • Segmentation fault — Ошибка сегментации (англ. Segmentation fault или сокращённо segfault)  ошибка программного обеспечения, возникающая при попытке обращения к недоступным для записи участкам памяти либо при попытке изменения памяти запрещённым способом. В системах… …   Википедия

  • segmentation fault — noun A software error that occurs when a program attempts to access a memory location that it is not permitted to access. Syn: access violation, segfault …   Wiktionary

  • Segmentation — may mean: *Market segmentation, in economics Biology *A morphogenesis process that divides a metazoan body into a series of semi repetitive segments *Segmentation (biology), the structure that results from said processComputing *Segmentation… …   Wikipedia

  • Segmentation (informatique) — Pour les articles homonymes, voir Segmentation. En informatique, la segmentation est une technique gérée par l unité de segmentation de la MMU, utilisée sur les systèmes d exploitation modernes, qui divise la mémoire physique (dans le cas de la… …   Wikipédia en Français

  • General protection fault — This article is about the x86 exception. For the webcomic, see General Protection Fault (webcomic). A general protection fault (GPF) in the Intel x86 and AMD x86 64 architectures, and other unrelated architectures, is a fault (a type of… …   Wikipedia

  • Segmention Fault — Bei Computern tritt eine Schutzverletzung (v. engl. segmentation fault oder segmentation violation, kurz auch segfault) auf, wenn ein Computerprogramm auf eine Ressource (insbesondere auf Speicher) zuzugreifen versucht, die vor einem solchen… …   Deutsch Wikipedia

  • Erreur De Segmentation — Pour les articles homonymes, voir Segmentation. Une erreur de segmentation (en anglais segmentation fault, parfois appelé en abrégé segfault), est un plantage d une application qui a tenté d accéder à un emplacement mémoire qui ne lui était pas… …   Wikipédia en Français


Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”

We are using cookies for the best presentation of our site. Continuing to use this site, you agree with this.