WHY DOES THIS STORY MATTER? If you ever observed any undefined behavior while coding in C/C++ you may have wondered why does C/C++ allow such type of behaviors. Here in this article we are gonna discuss the idea behind this. But wait, what is Undefined Behavior?
Okay, let me give a simple overview of Undefined behaviors that will help us to understand the concepts clearly.
Undefined Behavior in C/C++: In computer programming, undefined behavior is the result of executing a program whose behavior is prescribed to be unpredictable, in the language specification to which the computer code adheres.
Umm… Sounds pretty theoretical, right? Well, Undefined Behavior is one of those aspects of the C and C++ language that can be surprising to programmers coming from other languages (Where other languages try to hide it better).
Still confused! Let’s see some examples-
Example-1: Let’s try to run the below code where we are going beyond limit of signed int-
Output: x + 1: -2147483648 ( we got the minimum value of signed int)
Example-2: Trying to use uninitialized variable
Output: false value (This may differ in your compilers!)
More Examples: Some other such examples are- Performing divide by 0, Accessing value of NULL, Accessing array out of bound, Trying to modify a string literal etc.
Explanation: 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. Then the question again arises ‘Why does C/C++ allow Undefined Behavior?’
Well, there’s a trade-off. The designers of other languages (like Java and similar languages) didn’t want undefined behavior in their language. Allowing undefined behavior has the potential to improve performance, but the language designers prioritized safety and predictability higher.
For example, if you allocate an array in C, the data is undefined. In Java, all bytes must be initialized to 0 (or some other specified value). This means the runtime must pass over the array (an O(n) operation), while C can perform the allocation in an instant. So C will always be faster for such operations.
If the code using the array is going to populate it anyway before reading, this is basically wasted effort for Java. But in the case where the code read first, you get predictable results in Java but unpredictable results in C. So you may got the answer till now.
The other advantages are like-
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.
Reference — StackOverflow and Others
Hey, did you like the explanation?
If yes then don’t forget to follow exploreIT as I regularly come up with interesting topics related to programming. And of course, if you have any difficulty understanding the concepts or find anything wrong in the article please feel free to comment below!
Feet free to connect — https://www.linkedin.com/in/salman-shaikh-82989b1b9/
See you in the next article, Bye Bye!
Related Article(s)