why seg-fault? (etymology)
If you have ever programmed in C or C++, then you have likely run into a situation where you have received an error message like this:
$ ./runmyprogram
Segmentation fault
$
SIGSEGV, our favorite POSIX signal! Hopefully, you know what that means and why that happens. Coincidentally, I was asked this question just the other day (at a party no less) and figured I would share my insight. So, if you don’t know what a segfault is, I’ll hopefully shed a bit of light on the situation – essentially you messed up with your memory management. Somewhere in your program you decided to access memory that you do not have access to or you tried to access memory in a way that is illegal. You used a null pointer, used an un-initialized pointer, tried writing to read-only memory, tried overwriting another program’s (or the operating system)’s memory, etc. Essentially, you were a bad boy (or girl) and made the poor little CPU/Operating System angry. Segfaults can be really easy to debug, but usually it takes at least a bit of digging. Fire up your favorite debugger and go to work.
But wait a second, why is it called a Segmentation Fault? What does that even mean? By just dissecting the word, it is apparent. If you know a bit about the CPU architectures, you’d know that a fault is an exception thrown by the processor and that segmentation is a memory model that divides the address space into separate regions. Essentially, as mentioned before, you messed with a segment that isn’t yours to mess with. Segmentation is an older memory model originally introduced to the x86 architecture in the late 70s. x86 has since been expanded to support “multi-mode operation.” Segmentation is still supported in x86, but using strictly segmentation is a thing of the past that is rarely done in execution of modern programs (generally speaking). Instead, a flat memory model is used. Technically segmentation is still in use, but because the segment bases are treated as if they are zero and the limits are ignored, the segmentation mechanism is essentially disabled. Paging is used to enforce the protection limits on lesser privileged software and to allow the use of virtual memory. It should be noted, however, that in 64-bit mode segmentation is disabled.
The point of that blurb is this – since we are not actually using the segmentation model any more, why call it a “Segmentation Fault”? That question I really did not know the answer to (other than the obvious – no one wants to actually change it). The actual x86 exception is (and to my knowledge always has been) called a “General Protection Fault.” Logically, it may follow to just call this error by the x86 exception name. However, x86 is not the only architecture in existence. Other CPU architectures may simply name this fault differently. Thus, it would seem that the term “Segmentation Fault” was coined as it is a more generic and descriptive name. Although the segmentation model is no longer used, calling a region of memory a “segment” is not an invalid statement. So, the term lives on to continue to annoy programmers (well at least those programming on POSIX systems). Windows has dubbed the signal “STATUS_ACCESS_VIOLATION,” but I am fairly certain the error message is still something along the lines of “Segmentation Violation.”
Speaking of segfaults…
http://t-a-w.blogspot.com/2007/03/segfaulting-own-programs-for-fun-and.html