The new standard has different undefined behavior from the old one. The new sequencing rules, for example, mean that some arithmetic operations that used to be undefined (for such reasons as multiple writes between sequence points) are now defined.
So, what do we need to learn anew about undefined behavior?
Undefined Behavior in C and C++ 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.
It exists because of the syntax rules of C where a variable can be declared without init value. Some compilers assign 0 to such variables and some just assign a mem pointer to the variable and leave just like that. if program does not initialize these variables it leads to undefined behavior.
In computing (particularly, in programming), undefined value is a condition where an expression does not have a correct value, although it is syntactically correct. An undefined value must not be confused with empty string, Boolean "false" or other "empty" (but defined) values.
Undefined Behavior results in unpredicted behavior of the entire program. But in unspecified behavior, the program makes choice at a particular junction and continue as usual like originally function executes.
In my opinion the new rules are more complex to describe and to understand. For example consider that:
int x = 12;
x = x++ + 1; // undefined behaviour
x = ++x + 1; // valid
I would suggest to simply avoiding multiple side effects to the same variable in the same expression that is a rule simpler to understand. AFAIK C++0X changed some cases that were undefined behaviour in the past and that are now legal uses (for example the second of the two expressions above) but remember that there is and there will always be a difference between what is legal and what is moral ;-) ... no one is forcing you to use such things.
Actually in the above case seems that the validity of the second expression happened unintentionally as a side effect of fixing another issue (#222) in the language. The decision was to make the expression valid because it was considered that changing something from UB to well defined wasn't going to do any harm. I think however that while this didn't make any damage to programs (where of course UB is the worst possible problem), it actually made some damage to the language itself... changing a rule that was already complex to explain and understand to an even more obscure one.
IMO C++ is continuing its natural evolution from C into a language where a bunch of good-looking nice and logical statements can do wonderful things... and in which another bunch of equally good-looking, equally nice and equally logical statements can make your computer to explode instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With