Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the point of undefined behavior? [duplicate]

I can understand leaving something implementation defined, so that the particular people implementing it would know what's best to happen, but why would something ever be undefined behavior? Why not just say, anything else is implementation defined?

like image 797
northerner Avatar asked Feb 24 '17 11:02

northerner


People also ask

Why does undefined behavior exist?

Undefined behavior exists mainly to give the compiler freedom to optimize. One thing it allows the compiler to do, for example, is to operate under the assumption that certain things can't happen (without having to first prove that they can't happen, which would often be very difficult or impossible).

What are the consequences of undefined behavior?

Undefined behavior contributes to many serious problems, including security vulnerabilities. It's also, I believe, poorly understood, and discussions of it tend to become contentious.

Why does C++ have so much undefined behavior?

C and C++ have undefined behavior, because nobody's defined an acceptable alternative that allows them to do what they're intended to do. C# and Java take a different approach, but that approach fits poorly (if at all) with the goals of C and C++.

What is undefined behavior in programming?

In computer programming, undefined behaviour is defined as 'the result of compiling computer code which is not prescribed by the specs of the programming language in which it is written'.


2 Answers

There are a lot of cases in which ensuring an implementation defined behavior inevitably incurs overhead.

For example, how to make buffer overflow an implementation defined behavior? Maybe throw exception when it's detected, or maybe terminate the program? But doing that always requires bounds-checking.

C++ is designed in such a way that performance is almost never compromised.

There are languages like Go, Rust, and Java which do bounds-checking. And they all incur overhead to pay the price for the safety.

like image 147
cshu Avatar answered Oct 08 '22 15:10

cshu


Language specifications can be seen as a contract between the compiler writers and programmers. There are things that programmers assume that compiler would do (and not do). Compiler writers assume few things a programmer would do.

Compiler writer assumes programmer would write the code that has documented behavior or is free from undefined behavior. Based on that compiler writers can ignore some constructs and can make the compiled program faster as compared to the case if those constructs were not undefined. Some of such (as much as possible) undefined constructs are documented in the specifications to give the programmers an idea of such constructs.

Such undefined behavior exist to reduce the unnecessary complexity in implementation of specifications and sometimes to leave rooms for optimization.

like image 39
Mohit Jain Avatar answered Oct 08 '22 15:10

Mohit Jain