Consider the following C/C++ programs and try to guess the output?
Output of all of the above programs is unpredictable (or undefined). The compilers (implementing C/C++ standard) are free to do anything as these are undefined by the C and C++ standards.
Language like Java traps error as soon as they are found but language like C and C++ in few cases keep on executing the code in a silent but faulty manner which may result in unpredictable results. The program can crash with any type of error messages, or it can unknowingly corrupt the data which is a grave issue to deal with.
Importance of knowing about Undefined Behaviour
If a user starts learning in 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 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 compiler is changed/upgraded. For example last program produces 72 as output in most of the compilers, but implementing a software based on this assumption is not a good idea.
Undefined behaviors may also cause security vulnerabilities, especially due the cases when 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 greater performing array need not keep a look at the bounds, which avoid the needs for complex optimization pass to check such conditions outside loops. The tightly bound loops an 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 the 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 C/C++ compiler.
More examples of undefined behavior
- 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
- Customizing termination behavior for uncaught exception In C++
- cin get() in C++ with Examples
- Difference between Increment and Decrement Operators
- Map of Vectors in C++ STL with Examples
- Problem in comparing Floating point numbers and how to compare them correctly?
- exit(0) vs exit(1) in C/C++ with Examples
- Predefined Macros in C with Examples
- How to create GUI in C programming using GTK Toolkit
- C++ program to print all Even and Odd numbers from 1 to N
- Reverse the content of a file and store it in another
- ctype.h(<cctype>) library in C/C++ with Examples
- Minimum cells to be flipped to get a 2*2 submatrix with equal elements
- Nested Loops in C++ with Examples
- Slack Bytes in Structures : Explained with Example
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.