# Is segmentation fault actual undefined behavior in C++?

C++Server Side ProgrammingProgramming

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 −

#include<iostream>
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 − http://blog.llvm.org/2011/05/what-every-c-programmer-should-know.html

Published on 28-Feb-2018 15:07:56