Undefined behavior

In the early versions of C, undefined behavior's primary advantage was the production of performant compilers for a wide variety of machines: a specific construct could be mapped to a machine-specific feature, and the compiler did not have to generate additional code for the runtime to adapt the side effects to match semantics imposed by the language.However, progressive standardization of the platforms has made this less of an advantage, especially in newer versions of C. Now, the cases for undefined behavior typically represent unambiguous bugs in the code, for example indexing an array outside of its bounds.The compiler can also remove explicit checks that may have been in the source code, without notifying the programmer; for example, detecting undefined behavior by testing whether it happened is not guaranteed to work, by definition.This makes it hard or impossible to program a portable fail-safe option (non-portable solutions are possible for some constructs).It is the responsibility of the programmer to write code that never invokes undefined behavior, although compiler implementations are allowed to issue diagnostics when this happens.Thus the if statement, including the call to the function bar, can be ignored by the compiler since the test expression in the if has no side effects and its condition will never be satisfied.The code is therefore semantically equivalent to: Had the compiler been forced to assume that signed integer overflow has wraparound behavior, then the transformation above would not have been legal.For example, another function may call the above function: The compiler is free to optimize away the while-loop here by applying value range analysis: by inspecting foo(), it knows that the initial value pointed to by ptrx cannot possibly exceed 47 (as any more would trigger undefined behavior in foo()); therefore, the initial check of *ptrx > 60 will always be false in a conforming program.Hence, it is common for programmers, even experienced ones, to rely on undefined behavior either by mistake, or simply because they are not well-versed in the rules of the language that can span hundreds of pages.
Undefined valueUnspecified behaviorcomputer programminglanguage specificationprogramming languagesource codeplatformtranslatorC programming communitycomp.std.cside effectscompilersplatformsindexing an arrayruntimecompilerprogram transformationsdon't-care termsinstruction setmemory protectionoperating systemsupervisor modetoolchaininterpreterexecutable codesemantic gapportableinteger overflowwraparoundinliningvalue range analysisseparately compiled object fileprocessor register64-bitmachine codestatic program analysisfuzzingsecurityweb browsersLinux Weekly NewsPathScale CMicrosoft Visual C++ 2005automatic variabledivision by zerobuffer overflownull pointerdereferencingstring literalpointerssequence pointsbitwise shiftingbitwise operatorssizeofHalt and Catch FireJargon FileYouTubeISO/IEC 14882:2003(E): Programming Languages – C++ISO/IEC 14882:2003(E): Programming Languages - C++ISO/IEC 9899:2007(E): Programming Languages – CISO/IEC 9899:1999(E): Programming Languages – CPeter van der Linden