Segmentation Fault in C

In the C programming language, segmentation fault or segmentation violation or core dump is a condition that the hardware component raises to protect the memory by indicating to the operating system that the software has attempted to access the area of memory restricted for security purposes. In other words, it is a runtime error caused due to accessing memory that does not belong to specific software. A segmentation fault occurs due to a memory access violation.

A segmentation fault occurs when a snippet of code does a read and write operation where only a read operation should be conducted. It might occur even when there is stack overflow as it will request extra memory, which the CPU does not consist of. It can be defined as an error illustrating memory corruption.

Errors that lead to a segmentation fault

  • Format control strings – in the C programming language, the specific string’s reading and writing involves format specifiers or format control strings, for example, %d, %s, %f, and so on. These format control strings present in printf and scanf statements must be of equal number as initialized at the beginning of the program or are manipulated at later stages. The specifiers must match the data type of variable that will be printed or read.
  • Failure of initializing a pointer – while accessing a pointer in C, the variable must have a valid address, and only then can it be accessed. Ensuring that all the pointers are initialized to a valid memory area will not throw any segmentation fault in this case. Proper initialization must take place for the smooth execution of a program.
  • Acquiring memory beyond array limits – violating an array’s bounds while using array indexing leads to a segmentation fault. If the array limit exceeds the specified index value, it will throw an error, and it is defined as Undefined Behaviour (UB) as per ISO C standard. Undefined behavior results from executing a program whose behavior is prescribed to be an unpredictable one in a language to which the code adheres.
  • Imprecise usage of “address of (&)” and “dereferencing (*)” operators – In C, once a pointer variable is declared, it must be initialized with a valid memory address. Ampersand (&) is also used to scan the user input; incorrect usage of “&” gives a possible bug. Dereferencing a variable is used to return the location of a variable.
  • Out of bound access – this event occurs when a value is accessed or changed at an index value greater than the array size. It will keep on accessing a memory space that specifically does not belong to the error hence throws an error.
  • Buffer overflow – buffer overflow or buffer overrun is a deviation in a program that occurs when writing data to a buffer. It overruns the buffer’s boundaries and overwrites other memory locations. Buffers are spaces of memory set aside to hold data while moving different program sections or between the programs. 
  • Trying to access memory that the program does not have access to, like kernel structures, etc., throws a violation error.
  • This error occurs during accessing a freed memory location, which is a memory piece previously used by some object but is free now.

Examples

  • Writing to read-only memory.
  • Scanf usage
  • Out of array
  • Stack overflow

How to handle segmentation fault

Always check all the program code that uses pointers, subscripts of an array, dereferencing operator (*), and address operator (&). Segmentation fault usually means the program has an error and needs debugging. Sometimes, it is also possible for someone to intentionally cause such failure for testing, debugging platforms where direct access to a specific memory is necessary. In latter cases, the system must allow the program to run, albeit of the fault occurrence. In cases like so, as the system allows, it will be possible to handle the event and drastically increment the processor program counter to jump over the instruction to continue the ongoing execution.

Many operating systems (OS) such as Linux and windows have a special ability to handle a segmentation fault or a core dump. It depends on the architecture of that particular system. The executing program handles the event and extracts information about the state such as processor registering values, stack trace, line of code snippet where it was triggered, and so on.

Pin It on Pinterest

Share This