Undefined Behavior in C and C++ - GeeksforGeeks

Undefined Behavior in C and C++
Name Mangling and extern “C” in C++
How does ‘void*’ differ in C and C++?
Write a program that produces different results in C and C++
Type difference of character literals in C and C++
Difference Between C Structures and C++ Structures
Virtual Functions and Runtime Polymorphism in C++ | Set 1 (Introduction)
Virtual Function in C++
Polymorphism in C++
Encapsulation in C++
Abstraction in C++
Structure vs class in C++
Can a C++ class have an object of self type?
Why is the Size of an Empty Class Not Zero in C++?
Static data members in C++
Some interesting facts about static member functions in C++
Friend class and function in C++
Local Classes in C++
Nested Classes in C++
Simulating final Class in C++
Constructors in C++
Copy Constructor in C++
Destructors in C++
Virtual Destructor
Pure Virtual Destructor in C++
Arrays in C/C++
Dynamic Memory Allocation in C using malloc(), calloc(), free() and realloc()
std::sort() in C++ STL
Bitwise Operators in C/C++
What is Memory Leak? How can we avoid?
Undefined Behavior in C and C++
Name Mangling and extern “C” in C++
How does ‘void*’ differ in C and C++?
Write a program that produces different results in C and C++
Type difference of character literals in C and C++
Difference Between C Structures and C++ Structures
Virtual Functions and Runtime Polymorphism in C++ | Set 1 (Introduction)
Virtual Function in C++
Polymorphism in C++
Encapsulation in C++
Abstraction in C++
Structure vs class in C++
Can a C++ class have an object of self type?
Why is the Size of an Empty Class Not Zero in C++?
Static data members in C++
Some interesting facts about static member functions in C++
Friend class and function in C++
Local Classes in C++
Nested Classes in C++
Simulating final Class in C++
Constructors in C++
Copy Constructor in C++
Destructors in C++
Virtual Destructor
Pure Virtual Destructor in C++
Arrays in C/C++
Dynamic Memory Allocation in C using malloc(), calloc(), free() and realloc()
std::sort() in C++ STL
Bitwise Operators in C/C++
What is Memory Leak? How can we avoid?
Difficulty Level :
Easy
When we run a code, sometimes we see absurd results instead of expected output. So, in C/C++ programming, undefined behavior means when the program fails to compile, or it may execute incorrectly, either crashes or generates incorrect results, or when it may fortuitously do exactly what the programmer intended. Whenever the result of an executing program is unpredictable, it is said to have undefined behavior. 
As a C programmer, understanding undefined behavior is very important for optimal coding and for the program to yield a good efficiency, especially when it comes to there are C codes embedded in system design.
Division By Zero
int val = 5;
return val / 0; // undefined behavior
Memory accesses outside of array bounds
int arr[4] = {0, 1, 2, 3};
return arr[5];  // undefined behavior for indexing out of bounds
Signed integer overflow
int x = INT_MAX;
printf("%d", x + 1);     // undefined behavior
Null pointer dereference
val = 0;
int ptr = *val;        // undefined behavior for dereferencing a null pointer
Modification of string literal
char* s = "geeksforgeeks";
s[0] = 'e';               // undefined behavior  
Accessing a NULL Pointer, etc.
int* ptr = NULL;
printf("%d", *ptr);  // undefined behavior for accessing NULL Pointer
Sometimes compilers may diagnose simple errors, however, sometimes they are not designed to diagnose the undefined behavior.
Following are some C/C++ programs that showcase undefined behavior: 
Program 1:
C
// C Program to demonstrate// division by 0 #include <stdio.h> // Driver Codeint main(){    int x = 25, y = 0;    int z = x / y;    printf("%d", z);    return 0;}
Program 2:
C
// C Program to demonstrate// Uninitialized variables #include <stdio.h> // Driver Codeint main(){    bool val;    if (val)        printf("TRUE");    else        printf("FALSE");}
Program 3:
C
// C Program to demonstrate// accessing value of NULL// pointer #include <stdio.h> // Driver Codeint main(){    int* ptr = NULL;    printf("%d", *ptr);    return 0;}
Program 4:
C
// C program to demonstrate// accessing out of bound #include <stdio.h> // Driver Codeint main(){    int arr[5];     // We access arr[5] in last iteration.    for (int i = 0; i <= 5; i++)        printf("%d ", arr[i]);}
Program 5:
C
// C Program to demonstrate going// beyond limit of signed int #include <stdio.h> // Driver Codeint main(){    int x = INT_MAX;    printf("%d", x + 1);    return 0;}
Program 6:
C
// C Program to demonstrate trying to// modify a string literal #include <stdio.h> // Driver Codeint main(){    char* s = "geeksforgeeks";    s[0] = 'e';    return 0;}
Program 7:
C
// C Program to demonstrate modifying a variable// multiple times before a defined// sequence point #include <stdio.h> // Driver Codeint main(){    int i = 8;    int p = i++ * i++;    printf("%d\n", p);}
72
Explanation: The program produces 72 as output in most of the compilers, but implementing software based on this assumption is not a good idea. 
The output of all of the above programs is unpredictable (or undefined). The compilers (implementing the C/C++ standard) are free to do anything as these are undefined by the C and C++ standards. 
Language like Java, trap errors as soon as they are found but languages like C and C++ in a few cases keep on executing the code in a faulty manner which may result in unpredictable results. The program can crash with any type of error message, or it can unknowingly corrupt the data which is a grave issue to deal with. 
Importance of knowing about Undefined Behavior: If a user starts learning in a C/C++ environment and is unclear with the concept of undefined behavior then that can bring plenty of problems in the future like while debugging someone else’s code might be actually difficult in tracing the root to the undefined error.
Risks and Disadvantages of Undefined Behavior 
The programmers sometimes rely on a particular implementation (or compiler) of undefined behavior which may cause problems when the compiler is changed/upgraded. For example, the last program produces 72 as output in most of the compilers, but implementing software based on this assumption is not a good idea.
Undefined behaviors may also cause security vulnerabilities, especially due to the cases when an array out of bound is not checked (causes buffer overflow attack).
Advantages of Undefined Behavior 
C and C++ have undefined behaviors because it allows compilers to avoid lots of checks. Suppose a set of code with a greater performing array need not keep a look at the bounds, which avoids the need for a complex optimization pass to check such conditions outside loops. The tightly bound loops and speed up the program from thirty to fifty percent when it gains an advantage of the undefined nature of signed overflow, which is generally offered by the C compiler.
We also have another advantage of this as it allows us to store a variable’s value in a processor register and manipulate it over time that is larger than the variable in the source code.
It also helps in wrap-around then compile-time checks which would not be possible without the greater knowledge of the undefined behavior in the C/C++ compiler.
More Examples of undefined behavior  
Sequence Points in C | Set 1“delete this” in C++Passing NULL to printf in CAccessing array out of bounds in C/C++Use of realloc()Execution of printf with ++ operatorsVirtual destruction using shared_ptr in C++Virtual Destructor
Sequence Points in C | Set 1
“delete this” in C++
Passing NULL to printf in C
Accessing array out of bounds in C/C++
Use of realloc()
Execution of printf with ++ operatorsVirtual destruction using shared_ptr in C++
Virtual Destructor
chhabradhanvi
anshikajain26
cpp-advanced
C Language
C++
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Multidimensional Arrays in C / C++
Command line arguments in C/C++
rand() and srand() in C/C++
fork() in C
Core Dump (Segmentation fault) in C/C++
Vector in C++ STL
Inheritance in C++
Socket Programming in C/C++
Initialize a vector in C++ (6 different ways)
Operator Overloading in C++