Is segmentation fault actual undefined behavior in C++?

Undefined behavior is a way to give freedom to implementors (e.g. of compilers or of OSes) and to computers to do whatever they "want", in other words, to not care about consequences.

The cases in which segmentation fault occurs are transient in nature. They won't always result in a segmentation fault but can also run correctly(or at least appear to). For example, consider the following code fragment −

int main() {
    int arr[2];
    arr[0] = 0;
    arr[1] = 1;
    arr[2] = 2; // Undefined behaviour
    arr[3] = 3; // Undefined behaviour

This code might run correctly or result in a segmentation fault. It is not really defined and implementation-dependent. You can read more about undefined behavior here −

Updated on: 24-Jun-2020


Kickstart Your Career

Get certified by completing the course

Get Started