What is the difference between an indeterminate behaviour and an undefined behaviour in C++? Is this classification valid for C codes also?
-
@LuchianGrigore Yes, Thaks for pointing it – bubble Jun 28 '12 at 08:09
-
1Are you sure you mean *rule* and not *behavior*? – Luchian Grigore Jun 28 '12 at 08:10
-
@LuchianGrigore I think you are correct againg... It must be termed as behaviour. Or I should have talked about the document which converts it as a rule :) – bubble Jun 28 '12 at 08:12
-
The question is apparently about the use of the phrase "indeterminate behavior" in MISRA C++:2008. I suggest that the OP read that document to see what it says about it. – Jim Balter Jun 28 '12 at 11:02
-
@JimBalter this document is not freely available on net!!! Please share if you know what indeterminate behaviour is about – bubble Jun 28 '12 at 11:18
-
2Neither C nor C++ define the notion of indeterminate behavior. The word "indeterminate" can be encountered in such contexts as "indeterminate value". But there's no such thing as indeterminate behavior in neither C nor C++. – AnT stands with Russia Jul 07 '12 at 03:11
-
I think @AndreyT has it right: There is no such thing. That essentially makes this a dupe of an [FAQ entry](http://stackoverflow.com/questions/2397984/undefined-unspecified-and-implementation-defined-behavior). – sbi Jul 09 '12 at 10:22
3 Answers
The following remarks are based on the C standard, ISO-9899, rather than the C++ one, but the meanings are fundamentally the same (see sections 3.4 and 4 of the C standard; see also the C++ standard, ISO-14882, section 1.3; the latter document doesn't define 'unspecified value' as such, but does use that phrase later with the obvious meaning). The official standards documents are not free (indeed, they are expensive), but the links above are to the committee pages, and include free 'drafts' of the standard, which you can take to be essentially equivalent to the finalised standard text.
The terms describe a ladder of vagueness.
So, heading downwards....
Most of the time, the standard defines what should happen in a particular case: if you write c=a+b
and a
and b
are int
, then c
is their sum (modulo some details). This, of course, is the point of a standard.
Implementation-defined behaviour is where the standard lists two or more things which are allowed to happen in a particular case; it doesn't prescribe which one is preferred, but does demand that the implementation (the actual compiler which parses the C) makes a choice between the alternatives, does that same thing consistently, and that the implementation must document the choice it makes. For example, whether a single file can be opened by multiple processes is implementation-defined.
Unspecified behaviour is where the standard lists a couple of alternatives, each of which is therefore conformant with the standard, but goes no further. An implementation must choose one of the alternatives to pick in a particular case, but doesn't have to do the same thing each time, and doesn't have to commit itself in documentation to which choice it will make. For example, the padding bits in a struct
are unspecified.
Undefined behaviour is the most extreme case. Here, all bets are off. If the compiler, or the program it generates, runs into undefined behaviour, it can do anything: it can scramble memory, corrupt the stack, HCF or, in the standard extreme case, cause demons to fly out of your nose. But mostly it'll just crash. And all of these behaviours are conformant with the standard. For example, if a variable is declared both static int i;
and int i;
in the same scope, or if you write #include <'my file'.h>
, the effect is undefined.
There are analogous definitions for 'value'.
An unspecified value is a valid value, but the standard doesn't say what it is. Thus the standard might say that a given function returns an unspecified value. You can store that value and look at it if you want to, without causing an error, but it doesn't mean anything, and the function might return a different value next time, depending on the phase of the moon.
An implementation-defined value is like implementation-defined behaviour. Like unspecified, it's a valid value, but the implementation's documentation has to commit itself on what will be returned, and do the same thing each time.
An indeterminate value even more unspecified than unspecified. It's either an unspecified value or a trap representation. A trap representation is standards-speak for some magic value which, if you try to assign it to anything, results in undefined behaviour. This wouldn't have to be an actual value; probably the best way to think about it is "if C had exceptions, a trap representation would be an exception". For example, if you declare int i;
in a block, without an initialisation, the initial value of the variable i
is indeterminate, meaning that if you try to assign this to something else before initialising it, the behaviour is undefined, and the compiler is entitled to try the said demons-out-of-nose trick. Of course, in most cases, the compiler will do something less dramatic/fun, like initialise it to 0 or some other random valid value, but no matter what it does, you're not entitled to object.
The point of all this imprecision is to give maximal freedom to compiler writers. That's nice for compiler writers (and is one of the reasons it's reasonably easy to get a C compiler running on such a huge range of platforms), but it does make things rather more interesting than fun for the poor users.
Edit 1: to clarify indeterminate values.
Edit 2: to include a link to the C++ standard, and note that the committee drafts are essentially equivalent to the final standard, but free.

- 11,978
- 2
- 33
- 56
-
Thanks for the explaination. It seems that standard C does not mention anything on indeterminate behaviour(as evident from your description as well and the documents which I have) Can you tell exact difference between the indeterminate and undefined behavours.. – bubble Jun 28 '12 at 10:43
-
@bubble: A program with undefined behaviour is like a calculation that contains a division by zero; regardless of the result, it is meaningless. Unspecified things are meaningful. – molbdnilo Jun 28 '12 at 12:58
-
I like especially the idea that the compiler causes demons to fly out of the programmers nose – Michael Jul 06 '12 at 15:14
-
I've only once worked on an architecture that actually caught fire due to undefined behavior. – Crashworks Jul 07 '12 at 03:07
-
There is also a new distinction in recent C standards between "bounded undefined behavior" and "unbounded undefined behavior" for so-called "analyzable" implementations. For example, if you evaluate `i = (i++) + (i++);`, the behavior is "bounded undefined behavior" so it can (1) crash your program or (2) assign some completely arbitrary value to `i`, but it's not permitted to e.g. change the value of an unrelated variable and it's not permitted to make demons fly out your nose. – Dietrich Epp Jul 08 '12 at 20:13
-
That sounds interesting, but I can't find it in ISO-9899:2011. Is it one of the terms defined (by reference from section 3) in ISO/IEC 2382−1? – Norman Gray Jul 08 '12 at 23:18
I think the standard mentions undefined behaviour and indeterminate value. So one is about the behaviour and another about values.
These two are somewhat orthogonal, for example, the behaviour can still be well defined in the presence of indeterminate values.

- 131,725
- 17
- 180
- 271
-
Here the indeterminate reffers to behaviour. One of the document describes it as "Indeterminate are similar to Undefined, however, the C++ standard does not use the undefined keyword. " – bubble Jun 28 '12 at 08:21
-
The only mentions of behaviour that could be indeterminate that I can find in the standard is in the context of sequencing where to expressions may be 'indeterminately sequenced'. All other references are for indeterminate values. Two expressions A and B are 'indeterminately sequenced' if either A is sequenced before B or B is sequenced before A, but it is unspecified which; in addition, they _may not_ overlap. – boycy Jun 28 '12 at 08:32
-
@bubble I have trouble believing that any document says that. If you're going to quote something, you should be exact. – Jim Balter Jun 28 '12 at 10:42
-
@JimBalter I am reffering to MISRA 2008 standards for C++. I have used exactly the same wordings.. – bubble Jun 28 '12 at 10:46
-
I still doubt that MISRA 2008 uses that bad grammar, inappropriate capitalization, and refers to a "undefined keyword". Perhaps a URL and exact page and paragraph citation would convince me. – Jim Balter Jun 28 '12 at 10:54
-
1Ok, I've found the MISRA language: "This is similar to Undefined Behavior, but there the C++ Standard omits an explicit definition of behavior." -- So I was right that you were not quoting it correctly. And your question never mentions MISRA, which is rather bad. – Jim Balter Jun 28 '12 at 11:00
-
@JimBalter the document which I have mentions the exact same thing which I copy pasted here. I hope the indeterminate behavour is an attribute of the C++ laguage rather than MISRA. Hence wanted to make ny question more generic. – bubble Jun 28 '12 at 11:03
-
1@JimBalter You may be right in the exact statement since I my document is a derivative of the original one, provided by a tool vendor for the purpose of use with the tool. – bubble Jun 28 '12 at 11:07
-
EDIT 1: The last drafts of C11 and C++11 are available online here: C11 draft N1570 and C++11 draft n3242 if you don't have a copy of the final standards and wonderful what they look like. (Other adjustments to text appearance and some wording/grammar edits have been done.)
EDIT 2: Fixed all occurrences of "behaviour" to be "behavior" to match the standard.
Searching the C++11 and C11 standards there are no matches for indeterminate rule or undefined rule. There are terms like indeterminate value, indeterminately sequenced, indeterminate uninitialized, etc.
If talk of traps and exceptions seems weird in Norman Gray's answer, know that those terms do reflect the relevant definitions in Section 3 in the C11 standard.
C++ relies on C's definitions. Many useful definitions concerning types of behaviour can be found in C11's Section 3 (in C11). For example, indeterminate value is defined in 3.19.2. Do take note that C11's Section 2 (Normative References) provides other sources for additional terminology interpretation and Section 4 defines when cases such as undefined behavior occur as a result of not complying with the standard.
C11's section 3.4 defines behavior, 3.4.1 defines implementation-defined behavior, 3.4.2 defines locale-specific behavior, 3.4.3 defines undefined behavior, 3.4.4 defines unspecified behavior. For value (Section 3.19), there are implementation-defined value, indeterminate value, and unspecified value.
Loosely speaking, the term indeterminate refers to an unspecified/unknown state that by itself doesn't result in undefined behavior. For example, this C++ code involves an indeterminate value: { int x = x; }. (This is actually an example in the C++11 standard.) Here x is defined to be an integer first but at that point it does not have a well-defined value --then it is initialized to whatever (indeterminate/unknown) value it has!
The well-known term undefined behavior is defined in 3.4.3 in C11 and refers to any situation of a
nonportable or erroneous program construct or of erroneous data, for which this International Standard imposes no requirements
In other words undefined behavior is some error (in logic or state) and whatever happens next is unknown! So one could make an undefined [behavior] rule that states: avoid undefined behavior when writing C/C++ code! :-)
An indeterminate [behavior] rule would be to state: avoid writing indeterminate code unless it is needed and it does not affect program correctness or portability. So unlike undefined behavior, indeterminate behavior does not necessarily imply that code/data is erroneous, however, its subsequent use may or may not be erroneous --so care is required to ensure program correctness is maintained.
Other terms like indeterminately sequenced are in the body text (e.g., C11 5.1.2.3 para 3; C++11, section 1.9 para. 13; i.e., in [intro.executation]). (As you might guess, it refers an unspecified order of operational steps.)
IMO if one is interested in all of these nuances, acquiring both the C++11 and C11 standards is a must. This will permit one to explore to the desired level-of-detail needed with definitions, etc. If you don't have such the links provided herein will help you explore such with the last published draft C11 and C++11 standards.

- 1,283
- 11
- 10
-
Note that the C and C++ standards use the US spelling "behavior", not the UK spelling "behaviour". (Unless the BSI version of the standard changes it.) – Keith Thompson Jul 07 '12 at 02:57
-
@KeithThompson Good point. I fixed all occurrences of the word "behaviour" to use the US spelling used in the standard, i.e., "behaviour". Thanks! – Paul Preney Jul 08 '12 at 19:40
-
@KeithThompson do you agree that C++ relies on the C standard for the definition of indeterminate value etc...? – Shafik Yaghmour Apr 12 '14 at 02:23
-
2@ShafikYaghmour: No, the C++ standard has its own definitions (which happen to be similar to those in the C standard). As far as I know, the only things for which the C++ standard defers to the C standard are parts of the standard library. – Keith Thompson Apr 12 '14 at 05:45
-
@KeithThompson so Lightness believes [this is ambiguous](http://stackoverflow.com/questions/23020323/can-we-apply-content-not-explicitly-cited-from-the-normative-references-to-the-c#comment35236328_23021390) ... One day I will ask a question that has a simple answer. – Shafik Yaghmour Apr 14 '14 at 13:04
-
1"C++ relies on C's definitions." No, not at all. See `[defns.impl.defined]`, `[defns.undefined]`, and `[defns.unspecified]` in section 1. C++03 or C++11, both have them. – Ben Voigt May 13 '14 at 23:29