HOME

TheInfoList



OR:

In
computer programming Computer programming or coding is the composition of sequences of instructions, called computer program, programs, that computers can follow to perform tasks. It involves designing and implementing algorithms, step-by-step specifications of proc ...
, unspecified behavior is behavior that may vary on different implementations of a
programming language A programming language is a system of notation for writing computer programs. Programming languages are described in terms of their Syntax (programming languages), syntax (form) and semantics (computer science), semantics (meaning), usually def ...
. A program can be said to contain unspecified behavior when its
source code In computing, source code, or simply code or source, is a plain text computer program written in a programming language. A programmer writes the human readable source code to control the behavior of a computer. Since a computer, at base, only ...
may produce an
executable In computer science, executable code, an executable file, or an executable program, sometimes simply referred to as an executable or binary, causes a computer "to perform indicated tasks according to encoded instruction (computer science), in ...
that exhibits different behavior when compiled on a different
compiler In computing, a compiler is a computer program that Translator (computing), translates computer code written in one programming language (the ''source'' language) into another language (the ''target'' language). The name "compiler" is primaril ...
, 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.ISO/IEC (2009-05-29). ''ISO/IEC PDTR 24772.2: Guidance to Avoiding Vulnerabilities in Programming Languages through Language Selection and Use'' 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 causing portability problems.


Definition

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. 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. In the C and C++ languages, such non- portable constructs are generally grouped into three categories: Implementation-defined, unspecified, and undefined behavior. 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."
ISO The International Organization for Standardization (ISO ; ; ) is an independent, non-governmental, international standard development organization composed of representatives from the national standards organizations of member countries. Me ...
/ IEC (2003). '' ISO/IEC 14882:2003(E): Programming Languages - C++ §1.3.13 unspecified behavior efns.unspecified'
The C++ Standard also notes that the range of possible behaviors is usually provided. Unlike implementation-defined behavior, there is no requirement for the implementation to document its behavior. 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".
ISO The International Organization for Standardization (ISO ; ; ) is an independent, non-governmental, international standard development organization composed of representatives from the national standards organizations of member countries. Me ...
/ IEC (1999). '' ISO/IEC 9899:1999(E): Programming Languages - C §3.4.4 para 1''
Unspecified behavior is different from undefined 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.
ISO The International Organization for Standardization (ISO ; ; ) is an independent, non-governmental, international standard development organization composed of representatives from the national standards organizations of member countries. Me ...
/ IEC (2003). '' ISO/IEC 14882:2003(E): Programming Languages - C++ §1.3.12 undefined behavior efns.undefined'


Implementation-defined behavior

C and C++ distinguish ''implementation-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


Order of evaluation of subexpressions

Many programming languages do not specify the order of evaluation of the sub-expressions of a complete expression. This non-determinism can allow optimal implementations for specific platforms e.g. to utilise parallelism. If one or more of the sub-expressions has
side effect In medicine, a side effect is an effect of the use of a medicinal drug or other treatment, usually adverse but sometimes beneficial, that is unintended. Herbal and traditional medicines also have side effects. A drug or procedure usually use ...
s, then the result of evaluating the full-expression may be different depending on the order of evaluation of the sub-expressions. For example, given a = f(b) + g(b);, where f and g both modify b, the result stored in a may be different depending on whether f(b) or g(b) is evaluated first. In the C and C++ languages, this also applies to function arguments. Example: #include int f() int g() int sum(int i, int j) int main() The resulting program will write its two lines of output in an unspecified order. In some other languages, such as
Java Java is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea (a part of Pacific Ocean) to the north. With a population of 156.9 million people (including Madura) in mid 2024, proje ...
, the order of evaluation of operands and function arguments is explicitly defined.
James Gosling James Arthur Gosling (born 19 May 1955) is a Canadian computer scientist, best known as the founder and lead designer behind the Java (programming language), Java programming language. Gosling was elected a member of the National Academy of E ...
,
Bill Joy William Nelson Joy (born November 8, 1954) is an American computer engineer and venture capitalist. He co-founded Sun Microsystems in 1982 along with Scott McNealy, Vinod Khosla, and Andy Bechtolsheim, and served as Chief Scientist and CTO ...
,
Guy Steele Guy Lewis Steele Jr. (; born October 2, 1954) is an American computer scientist who has played an important role in designing and documenting several computer programming languages and technical standards. Biography Steele was born in Missouri ...
, and Gilad Bracha (2005). ''The Java Language Specification'', Third Edition. Addison-Wesley.


See also

*
Software portability Software consists of computer programs that instruct the execution of a computer. Software also includes design documents and specifications. The history of software is closely tied to the development of digital computers in the mid-20th ...
* Undefined behavior


References

{{reflist Articles with example C code Articles with example C++ code Computer programming Programming language implementation