Segmentation fault is the operating system sending a signal to the program saying that it has detected illegal memory access and is prematurely terminating the program to prevent memory from being corrupted.

“segmentation” is the concept of each process on your computer having its own distinct virtual address space. Thus, when Process A reads memory location 0x875, it reads information residing at a different physical location in RAM than when Process B reads its own 0x875.

A segmentation fault is when your program attempts to access memory it has either not been assigned by the operating system or is otherwise not allowed to access. All modern operating systems support and use segmentation, and so all can produce a segmentation fault.

To deal with a segmentation fault, fix the code causing it. It is generally indicative of poor programming, especially boundary-condition errors, incorrect pointer manipulation, or invalid assumptions about shared libraries. Sometimes segfaults, like any problem, may be caused by faulty hardware, but this is usually not the case.

Segmentation fault is also caused by hardware failures, in this case, the RAM memories. This is the less common cause, but if you don’t find an error in your code, maybe a memtest could help you.

In computing, a segmentation fault (often shortened to segfault) or access violation is a fault raised by hardware with memory protection, notifying an operating system (OS) about a memory access violation.

Some typical causes of a segmentation fault:

  • Dereferencing NULL pointers – this is special-cased by memory management hardware
  • Attempting to access a nonexistent memory address (outside process’s address space)
  • Attempting to access memory the program does not have rights to (such as kernel structures in process context)
  • Attempting to write read-only memory (such as code segment)

Programming errors that result in invalid memory access:

  • Dereferencing or assigning to an uninitialized pointer (wild pointer, which points to a random memory address)
  • Dereferencing or assigning to a freed pointer (dangling pointer, which points to memory that has been freed/deallocated/deleted)
  • A buffer overflow.
  • A stack overflow.
  • Attempting to execute a program that does not compile correctly. (Some compilers will output an executable file despite the presence of compile-time errors.)

Examples for segmentation fault:

There are many ways to get a segfault, we’ve mentioned simple examples from the lower-level languages such as C and C++.

A common way to get a segfault is to dereference a null pointer:

int *gs = NULL;
*gs = 1;

Another segfault happens when you try to write to a portion of memory that was marked as read-only:

char *gs = "Foo"; // Compiler marks the given constant string as read-only
*gs = 'b'; // Here, it is illegal and results in a segfault

Dangling pointer points to a thing that does not exist anymore, like here:

char *gs = NULL;
    char c;
    gs = &c;
// Now, gs is dangling

The pointer “gs” dangles because it points to the character variable “c” that ceased to exist after the block ended. And when you try to dereference dangling pointer (like *gs='A'), you would probably get a segfault.


Leave a Reply

Your email address will not be published. Required fields are marked *

Sign In


Reset Password

Please enter your username or email address, you will receive a link to create a new password via email.