-2

Is there is any specific reason behind undefined behavior in C and C++?

Why are some features left undefined?

herohuyongtao
  • 49,413
  • 29
  • 133
  • 174
VINOTH ENERGETIC
  • 1,775
  • 4
  • 23
  • 38
  • what features do you mean? – user1810087 Feb 26 '14 at 14:50
  • 8
    relevant: [Philosophy behind Undefined Behavior](http://programmers.stackexchange.com/questions/99692/philosophy-behind-undefined-behavior) it really boils down to a design choice as I stated [here](http://stackoverflow.com/questions/21898296/what-is-the-difference-in-how-c-and-java-handle-out-of-bounds-arrays-access/21898305#21898305) comparing Java and C and C++. – Shafik Yaghmour Feb 26 '14 at 14:51
  • 1
    It forgive freedom by implementation. For efficiency. – BLUEPIXY Feb 26 '14 at 14:54
  • @user1810087 see some of that in my blog http://arraynotfound.blogspot.in/2014/02/undefined-behavior-in-programming.html – VINOTH ENERGETIC Feb 26 '14 at 14:56
  • Because the standard can't cover everything in the known world. If you look at your own native language and compare it with others, you'll find that your language lacks words for certain things. If your language for example lacks the word 'hi' and someone speaks the English word 'hi' to you, there is no telling how you will react. Perhaps it means something completely different in your native language, meaning that you will react in a completely unpredicted way. Perhaps you understand just fine what was said. Perhaps you just stand there clueless with your mind numb. -> – Lundin Feb 26 '14 at 15:24
  • It is outside the scope of the language. – Lundin Feb 26 '14 at 15:25
  • 1
    Take a list of say, a dozen commonly experienced cases of undefined behaviour. Now outline a possible implementation that would catch this behaviour at runtime or even at compile time. Now look at how costly this is in either compile or runtime (assuming you even found a solution). Now ask people what they would like to have: the safety, or the speed. – PlasmaHH Feb 26 '14 at 15:34
  • @PlasmaHH as I mentioned a lot is also design choice, Java chooses to make many behaviors that C and C++ define as UB as well defined. Also, interesting to note that [C++ carved out an exception of UB within a constexpr](http://stackoverflow.com/questions/21319413/why-do-constant-expressions-have-an-exclusion-for-undefined-behavior) and both clang and gcc with the exception of shifts in gcc will catch UB in a constexpr at compile time. Also gcc now has a [UB sanitizer](http://gcc.gnu.org/onlinedocs/gcc/Debugging-Options.html#index-fsanitize_003dundefined-596). – Shafik Yaghmour Feb 26 '14 at 15:46
  • @ShafikYaghmour: you might have noticed that what I wrote is essentially part of process of making a design choice. – PlasmaHH Feb 26 '14 at 15:57

1 Answers1

3

For some part at least, it was to allow a more efficient implementation.

A simple example: Function parameters. Their evaluation order in unspecifed, because some architectures could work better depending on how they made the calculations or the calling convention (registers, stack, etc.)

Medinoc
  • 6,577
  • 20
  • 42
  • 4
    Unspecified behavior is not the same as undefined behavior. Unspecified means that the implementation _must_ implement a certain feature, in one of several possible ways, but it need not document how it does it. Undefined behavior means that something is outside the scope of the standard and thus anything can happen. – Lundin Feb 26 '14 at 15:03
  • For example, an implementation may evaluate the order of parameters in any way it pleases, but it must always evaluate all parameters. No sudden unexpected failure or crash is allowed to occur during the evaluation of these - the program must execute in a stable manner. If the function however returns a pointer to a local variable and dereferences it, then it is UB and there are no guarantees of what will happen, the program is free to halt & catch fire. – Lundin Feb 26 '14 at 15:09
  • 1
    @Lundin: `int* a(nullptr); f(a = new int(42), *a);` is UB because evaluation order is unspecified... – Jarod42 Feb 26 '14 at 15:26