Common Reasons for Segmentation Faults in C/C++



The main reason for segmentation fault is accessing memory that is either not initialized, out of bounds for your program or trying to modify string literals. These may cause a segmentation fault though it is not guaranteed that they will cause a segmentation fault. Here are some of the common reasons for segmentation faults −

  • Accessing an array out of bounds
  • Dereferencing NULL pointers
  • Dereferencing freed memory
  • Dereferencing uninitialized pointers
  • Incorrect use of the "&" (address of) and "*" (dereferencing) operators
  • Improper formatting specifiers in printf and scanf statements
  • Stack overflow
  • Writing to read-only memory
Updated on: 2020-01-27T12:38:34+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements