Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Unspecified behavior

From Wikipedia, the free encyclopedia
(Redirected fromUnspecified behaviour)
Differing behavior by identically-coded programs when compiled
Not to be confused withUndefined behavior.

Incomputer programming,unspecified behavior is behavior that may vary on different implementations of aprogramming language.[clarification needed] Aprogram can be said to contain unspecified behavior when itssource code may produce anexecutable that exhibits different behavior when compiled on a differentcompiler, or on the same compiler with different settings, or indeed in different parts of the same executable. While the respective language standards or specifications may impose a range of possible behaviors, the exact behavior depends on the implementation and may not be completely determined upon examination of the program's source code.[1] Unspecified behavior will often not manifest itself in the resulting program's external behavior, but it may sometimes lead to differing outputs or results, potentially causingportability problems.

Definition

[edit]

To enable compilers to produce optimal code for their respective target platforms, programming language standards do not always impose a certain specific behavior for a given source code construct.[2] Failing to explicitly define the exact behavior of every possible program is not considered an error or weakness in the language specification, and doing so would be infeasible.[1] In theC andC++ languages, such non-portable constructs are generally grouped into three categories: Implementation-defined, unspecified, andundefined behavior.[3]

The exact definition of unspecified behavior varies. In C++, it is defined as "behavior, for a well-formed program construct and correct data, that depends on the implementation."[4] The C++ Standard also notes that the range of possible behaviors is usually provided.[4] Unlike implementation-defined behavior, there is no requirement for the implementation to document its behavior.[4] Similarly, the C Standard defines it as behavior for which the standard "provides two or more possibilities and imposes no further requirements on which is chosen in any instance".[5] Unspecified behavior is different fromundefined behavior. The latter is typically a result of an erroneous program construct or data, and no requirements are placed on the translation or execution of such constructs.[6]

Implementation-defined behavior

[edit]

C and C++ distinguishimplementation-defined behavior from unspecified behavior. For implementation-defined behavior, the implementation must choose a particular behavior and document it. An example in C/C++ is the size of integer data types. The choice of behavior must be consistent with the documented behavior within a given execution of the program.

Examples

[edit]

Order of evaluation of subexpressions

[edit]

Many programming languages do not specify the order of evaluation of the sub-expressions of a completeexpression. This non-determinism can allow optimal implementations for specific platforms e.g. to utilise parallelism. If one or more of the sub-expressions hasside effects, then the result of evaluating the full-expression may be different depending on the order of evaluation of the sub-expressions.[1] For example, given

a=f(b)+g(b);

, wheref andg both modifyb, the result stored ina may be different depending on whetherf(b) org(b) is evaluated first.[1] In the C and C++ languages, this also applies to function arguments. Example:[2]

#include<iostream>intf(){std::cout<<"In f\n";return3;}intg(){std::cout<<"In g\n";return4;}intsum(inti,intj){returni+j;}intmain(){returnsum(f(),g());}

The resulting program will write its two lines of output in an unspecified order.[2] In some other languages, such asJava, the order of evaluation of operands and function arguments is explicitly defined.[7]

See also

[edit]

References

[edit]
  1. ^abcdISO/IEC (2009-05-29).ISO/IEC PDTR 24772.2: Guidance to Avoiding Vulnerabilities in Programming Languages through Language Selection and Use
  2. ^abcBecker, Pete (2006-05-16)."Living By the Rules".Dr. Dobb's Journal. Retrieved26 November 2009.
  3. ^Henricson, Mats; Nyquist, Erik (1997).Industrial Strength C++. Prentice Hall.ISBN 0-13-120965-5.
  4. ^abcISO/IEC (2003).ISO/IEC 14882:2003(E): Programming Languages - C++ §1.3.13 unspecified behavior [defns.unspecified]
  5. ^ISO/IEC (1999).ISO/IEC 9899:1999(E): Programming Languages - C §3.4.4 para 1
  6. ^ISO/IEC (2003).ISO/IEC 14882:2003(E): Programming Languages - C++ §1.3.12 undefined behavior [defns.undefined]
  7. ^James Gosling,Bill Joy,Guy Steele, andGilad Bracha (2005).The Java Language Specification, Third Edition. Addison-Wesley.ISBN 0-321-24678-0
Retrieved from "https://en.wikipedia.org/w/index.php?title=Unspecified_behavior&oldid=1315998534"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2025 Movatter.jp