What is a Segmentation Fault?

Article Details
  • Written By: Jessica Susan Reuter
  • Edited By: Allegra J. Lingo
  • Image By: ビッグアップジャパン
  • Last Modified Date: 22 November 2019
  • Copyright Protected:
    Conjecture Corporation
  • Print this Article
Free Widgets for your Site/Blog
The term "time immemorial" originally referred to the time before Richard I became King of England in July 1189.  more...

December 7 ,  1941 :  Japanese bombers attack Pearl Harbor.  more...

A segmentation fault is a computer error that occurs when a program attempts to access a region of memory in an invalid way. These errors can be read errors, where an attempt is made to read data that has not been properly initialized, or write errors, where a program attempts to write data to an incorrect location in memory. In computer parlance, the term is often shortened to segfault. Since segfaults can occur in both data reading and writing, they can occur in a variety of contexts and with different specific names, including bus errors, buffer overflows, page faults, and storage violations.

Although a segmentation fault can be created in some computer languages more easily than others, segfaults are not part of computer languages themselves. In theory, any computer language can cause a data writing error that spawns a segfault. Computer languages that allow a programmer to explicitly handle memory, such as C++, allow the user to introduce unintentional segfaults relatively easily with poor programming. Languages that do not allow explicit memory management, like Java, handle most memory issues without the programmer's interference, and therefore minimize segmentation faults by giving a programmer no way to purposefully access areas outside of allocated memory.


The ways in which a programmer can create a segmentation fault vary, depending on the manner in which data is being used. There are certain constructs that are easier to create this error with. One common culprit of a segfault is an array overflow error, in which specific data is contained in slots within a particular memory location but the programmer attempts to access a slot that does not exist. Segfaults spawned by these situations are often difficult to track down. Some computer languages have bounds checking, which anticipates this type of error and will not let the user attempt to access an invalid slot.

A less common way in which segfaults can be generated occurs when two computer languages pass data between each other using a proxy. In this context, a segmentation fault is particularly difficult to diagnose because it is not always clear which language or layer of programming code the error is coming from. Even if one layer has implicit bounds checking and doesn't allow segfaults in 99.9% of contexts, it can be a very time-consuming and tedious job to determine where the error came from.

The best way to avoid segmentation faults is to be very cautious with memory management. Most good programming practices recommend that users should explicitly free every byte of memory that is manually allocated in a program. This is often difficult to do, but doing it correctly increases a programmer's chances of creating a robust, error-free program.


You might also Like


Discuss this Article

Post your comments

Post Anonymously


forgot password?