HOME

TheInfoList



OR:

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. Most programming languages are text-based formal languages, but they may also be graphical. They are a kind of computer language. The description of a programming l ...
. A program can be said to contain unspecified behavior when its
source code In computing, source code, or simply code, is any collection of code, with or without comment (computer programming), comments, written using a human-readable programming language, usually as plain text. The source code of a Computer program, p ...
may produce an
executable In computing, 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 instructions", as opposed to a data fil ...
that exhibits different behavior when compiled on a different
compiler In computing, a compiler is a computer program that translates computer code written in one programming language (the ''source'' language) into another language (the ''target'' language). The name "compiler" is primarily used for programs that ...
, 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 Portability may refer to: *Portability (social security), the portability of social security benefits * Porting, the ability of a computer program to be ported from one system to another in computer science ** Software portability, the 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 Portable may refer to: General * Portable building, a manufactured structure that is built off site and moved in upon completion of site and utility work * Portable classroom, a temporary building installed on the grounds of a school to provide a ...
constructs are generally grouped into three categories: Implementation-defined, unspecified, and
undefined behavior In computer programming, undefined behavior (UB) is the result of executing a program whose behavior is prescribed to be unpredictable, in the language specification to which the computer code adheres. This is different from unspecified behavio ...
. 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 ISO is the most common abbreviation for the International Organization for Standardization. ISO or Iso may also refer to: Business and finance * Iso (supermarket), a chain of Danish supermarkets incorporated into the SuperBest chain in 2007 * Is ...
/ 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 ISO is the most common abbreviation for the International Organization for Standardization. ISO or Iso may also refer to: Business and finance * Iso (supermarket), a chain of Danish supermarkets incorporated into the SuperBest chain in 2007 * Is ...
/ IEC (1999). '' ISO/IEC 9899:1999(E): Programming Languages - C §3.4.4 para 1''
Unspecified behavior is different from
undefined behavior In computer programming, undefined behavior (UB) is the result of executing a program whose behavior is prescribed to be unpredictable, in the language specification to which the computer code adheres. This is different from unspecified behavio ...
. 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 ISO is the most common abbreviation for the International Organization for Standardization. ISO or Iso may also refer to: Business and finance * Iso (supermarket), a chain of Danish supermarkets incorporated into the SuperBest chain in 2007 * Is ...
/ 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 Expression may refer to: Linguistics * Expression (linguistics), a word, phrase, or sentence * Fixed expression, a form of words with a specific meaning * Idiom, a type of fixed expression * Metaphorical expression, a particular word, phrase, ...
. 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, whether therapeutic or adverse, that is secondary to the one intended; although the term is predominantly employed to describe adverse effects, it can also apply to beneficial, but unintended, consequence ...
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 other languages, such as
Java Java (; id, Jawa, ; jv, ꦗꦮ; su, ) is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea to the north. With a population of 151.6 million people, Java is the world's mo ...
, the order of evaluation of operands and function arguments is explicitly defined.
James Gosling James Gosling (born May 19, 1955) is a Canadian computer scientist, best known as the founder and lead designer behind the Java programming language. Gosling was elected a member of the National Academy of Engineering in 2004 for the conception ...
,
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 a ...
,
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 Gilad Bracha is a software engineer at F5 Networks, and formerly at Google, where he was on the Dart programming language team. He is creator of the Newspeak language, and co-author of the second and third editions of the Java Language Specificati ...
(2005). ''The Java Language Specification'', Third Edition. Addison-Wesley.


See also

*
Software portability A computer program is said to be portable if there is very low effort required to make it run on different platforms. The pre-requirement for portability is the generalized abstraction between the application logic and system interfaces. When ...
*
Undefined behavior In computer programming, undefined behavior (UB) is the result of executing a program whose behavior is prescribed to be unpredictable, in the language specification to which the computer code adheres. This is different from unspecified behavio ...


References

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