Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undefined vs. Unspecified vs. Implementation-defined behavior [duplicate]

Tags:

c++

c

Wikipedia has pages about undefined and unspecified behavior and links to them are plentifully used in comments and answers here, in SO.

Each one begins with a note to not be confused with other one but except one no very clear sentence they didn't point at the difference between them.

One of them gives an example (comparing addresses of 2 variables: &a < &b) with the comment that this will results in unspecified behavior in C++, undefined in C.

Is it possible to pinpoint the substantial difference between undefined and unspecified behavior in a clear, understandable manner?

like image 912
MarianD Avatar asked Dec 06 '22 14:12

MarianD


1 Answers

In short:

  • Undefined behaviour: this is not okay to do
  • Unspecified behaviour: this is okay to do, but the result could be anything*
  • Implementation-defined behaviour: this is okay to do, the result could be anything* but the compiler manual should tell you

Or, in quotes from the C++ standard (N4659 section 3, Terms and Definitions):

3.28 Undefined behavior: behavior for which this International Standard imposes no requirements

3.29 Unspecified behavior: behavior, for a well-formed program construct and correct data, that depends on the implementation

3.12 Implementation-defined behavior: behavior, for a well-formed program construct and correct data, that depends on the implementation and that each implementation documents


EDIT: *As pointed out by M.M in the comments, saying that the result of unspecified behaviour could be anything is not quite right. In fact as the standard itself points out, in a note for paragraph 3.29

The range of possible behaviors is usually delineated by this International Standard.

So in practise you have some idea of what the possible results are, but what exactly will happen depends on your compiler/compiler flags/platform/etc.

like image 167
Tristan Brindle Avatar answered Feb 16 '23 03:02

Tristan Brindle