Undefined behavior and sequence points
C++98 and C++03
This answer is for the older versions of the C++ standard. The C++11 and C++14 versions of the standard do not formally contain 'sequence points'; operations are 'sequenced before' or 'unsequenced' or 'indeterminately sequenced' instead. The net effect is essentially the same, but the terminology is different.
Disclaimer : Okay. This answer is a bit long. So have patience while reading it. If you already know these things, reading them again won't make you crazy.
Pre-requisites : An elementary knowledge of C++ Standard
What are Sequence Points?
The Standard says
At certain specified points in the execution sequence called sequence points, all side effects of previous evaluations
shall be complete and no side effects of subsequent evaluations shall have taken place. (§1.9/7)
Side effects? What are side effects?
Evaluation of an expression produces something and if in addition there is a change in the state of the execution environment it is said that the expression (its evaluation) has some side effect(s).
int x = y++; //where y is also an int
In addition to the initialization operation the value of
y gets changed due to the side effect of
So far so good. Moving on to sequence points. An alternation definition of seq-points given by the comp.lang.c author
Sequence point is a point in time at which the dust has settled and all side effects which have been seen so far are guaranteed to be complete.
What are the common sequence points listed in the C++ Standard?
at the end of the evaluation of full expression (
§1.9/16) (A full-expression is an expression that is not a subexpression of another expression.)1
int a = 5; // ; is a sequence point here
in the evaluation of each of the following expressions after the evaluation of the first expression (
a && b (§5.14)
a || b (§5.15)
a ? b : c (§5.16)
a , b (§5.18)(here a , b is a comma operator; in
,is not a comma operator, it's merely a separator between the arguments
a++. Thus the behaviour is undefined in that case (if
ais considered to be a primitive type))
at a function call (whether or not the function is inline), after the evaluation of all function arguments (if any) which
takes place before execution of any expressions or statements in the function body (
1 : Note : the evaluation of a full-expression can include the evaluation of subexpressions that are not lexically
part of the full-expression. For example, subexpressions involved in evaluating default argument expressions (8.3.6) are considered to be created in the expression that calls the function, not the expression that defines the default argument
2 : The operators indicated are the built-in operators, as described in clause 5. When one of these operators is overloaded (clause 13) in a valid context, thus designating a user-defined operator function, the expression designates a function invocation and the operands form an argument list, without an implied sequence point between them.
What is Undefined Behaviour?
The Standard defines Undefined Behaviour in Section
behavior, such as might arise upon use of an erroneous program construct or erroneous data, for which this International Standard imposes no requirements 3.
Undefined behavior may also be expected when this
International Standard omits the description of any explicit definition of behavior.
3 : permissible undefined behavior ranges from ignoring the situation completely with unpredictable results, to behaving during translation or program execution in a documented manner characteristic of the environment (with or with-
out the issuance of a diagnostic message), to terminating a translation or execution (with the issuance of a diagnostic message).
In short, undefined behaviour means anything can happen from daemons flying out of your nose to your girlfriend getting pregnant.
What is the relation between Undefined Behaviour and Sequence Points?
Before I get into that you must know the difference(s) between Undefined Behaviour, Unspecified Behaviour and Implementation Defined Behaviour.
You must also know that
the order of evaluation of operands of individual operators and subexpressions of individual expressions, and the order in which side effects take place, is unspecified.
int x = 5, y = 6;
int z = x++ + y++; //it is unspecified whether x++ or y++ will be evaluated first.
Another example here.
Now the Standard in
- Between the previous and next sequence point a scalar object shall have its stored value modified at most once by the evaluation of an expression.
What does it mean?
Informally it means that between two sequence points a variable must not be modified more than once.
In an expression statement, the
next sequence point is usually at the terminating semicolon, and the
previous sequence point is at the end of the previous statement. An expression may also contain intermediate
From the above sentence the following expressions invoke Undefined Behaviour:
i++ * ++i; // UB, i is modified more than once btw two SPs
i = ++i; // UB, same as above
++i = 2; // UB, same as above
i = ++i + 1; // UB, same as above
++++++i; // UB, parsed as (++(++(++i)))
i = (i, ++i, ++i); // UB, there's no SP between `++i` (right most) and assignment to `i` (`i` is modified more than once btw two SPs)
But the following expressions are fine:
i = (i, ++i, 1) + 1; // well defined (AFAIK)
i = (++i, i++, i); // well defined
int j = i;
j = (++i, i++, j*i); // well defined
- Furthermore, the prior value shall be accessed only to determine the value to be stored.
What does it mean? It means if an object is written to within a full expression, any and all accesses to it within the same expression must be directly involved in the computation of the value to be written.
For example in
i = i + 1 all the access of
i (in L.H.S and in R.H.S) are directly involved in computation of the value to be written. So it is fine.
This rule effectively constrains legal expressions to those in which the accesses demonstrably precede the modification.
std::printf("%d %d", i,++i); // invokes Undefined Behaviour because of Rule no 2
a[i] = i++ // or a[++i] = i or a[i++] = ++i etc
is disallowed because one of the accesses of
i (the one in
a[i]) has nothing to do with the value which ends up being stored in i (which happens over in
i++), and so there's no good way to define--either for our understanding or the compiler's--whether the access should take place before or after the incremented value is stored. So the behaviour is undefined.
Example 3 :
int x = i + i++ ;// Similar to above
Follow up answer for C++11 here.
What is the difference between a sequence point and operator precedence?
Operator precedence (and associativity) state the order in which an expression is parsed and executed. However, this says nothing about the order of evaluation of the operands, which is a different term. Example:
a() + b() * c()
Operator precedence dictates that the result of
b() and the result of
c() must be multiplied before added together with the result of
However, it says nothing about the order in which these functions should be executed. The order of evaluation of each operator specifies this. Most often, the order of evaluation is unspecified (unspecified behavior), meaning that the standard lets the compiler do it in any order it likes. The compiler need not document this order nor does it need to behave consistently. The reason for this is to give compilers more freedom in expression parsing, meaning faster compilation and possibly also faster code.
In the above example, I wrote a simple test program and my compiler executed the above functions in the order
c(). The fact that the program needs to execute both
c() before it can multiply the results, doesn't mean that it must evaluate those operands in any given order.
This is where sequence points come in. It is a given point in the program where all previous evaluations (and operations) must be done. So sequence points are mostly related to order of evaluation and not so much operator precedence.
In the example above, the three operands are unsequenced in relation to each other, meaning that no sequence point dictates the order of evaluation.
Therefore it turns problematic when side effects are introduced in such unsequenced expressions. If we write
i++ + i++ * i++, then we still don't know the order in which these operands are evaluated, so we can't determine what the result will be. This is because both
* have unspecified/unsequenced order of evaluation.
Had we written
i++ || i++ && i++, then the behavior would be well-defined, because the
|| specifies the order of evaluation to be left-to-right and there is a sequence point between the evaluation of the left and the right operand. Thus
if(i++ || i++ && i++) is perfectly portable and safe (although unreadable) code.
As for the expression
i = i++;, the problem here is that the
= is defined as (6.5.16):
The side effect of updating the stored value of the left operand is sequenced after the value computations of the left and right operands. The evaluations of the operands are unsequenced.
This expression is actually close to be well-defined, because the text actually says that the left operand should not be updated before the right operand is computed. The problem is the very last sentence: the order of evaluation of the operands is unspecified/unsequenced.
And since the expression contains the side effect of
i++, it invokes undefined behavior, since we can't know if the operand
i or the operand
i++ is evaluated first.
(There's more to it, since the standard also says that an operand should not be used twice in an expression for unrelated purposes, but that's another story.)
Undefined behavior and sequence points reloaded
It looks like the code
Works perfectly fine with regards to sequence points. Section 1.9.17 of the C++ ISO standard says this about sequence points and function evaluation:
When calling a function (whether or not the function is inline), there is a sequence point after the evaluation of all function arguments (if any) which takes place before execution of any expressions or statements in the function body. There is also a sequence point after the copying of a returned value and before the execution of any expressions outside the function.
This would indicate, for example, that the
i.operator ++() as the parameter to
operator += has a sequence point after its evaluation. In short, because overloaded operators are functions, the normal sequencing rules apply.
Great question, by the way! I really like how you're forcing me to understand all the nuances of a language that I already thought I knew (and thought that I thought that I knew). :-)
Differences in C and C++ with sequence points and UB
There are two parts to this question, we can tackle a comparison of sequence points rules without much trouble. This does not get us too far though, C and C++ are different languages which have different standards(the latest C++ standard is almost twice as large as the the latest C standard) and even though C++ uses C as a normative reference it would be incorrect to quote the C++ standard for C and vice versa, regardless how similar certain sections may be. The C++ standard does explicitly reference the C standard but that is for small sections.
The second part is a comparison of undefined behavior between C and C++, there can be some big differences and enumerating all the differences in undefined behavior may not be possible but we can give some indicative examples.
Since we are talking about sequence points then this is covering pre C++11 and pre C11. The sequence point rules do not differ greatly as far as I can tell between C99 and Pre C++11 draft standards. As we will see in some of the example I give of differing undefined behavior the sequence point rules do not play a part in them.
The sequence points rules are covered in the closest draft C++ standard to C++03 section
1.9 Program execution which says:
- There is a sequence point at the completion of evaluation of each full-expression12).
- When calling a function (whether or not the function is inline), there is a sequence point after the evaluation of all
function arguments (if any) which takes place before execution of any expressions or statements in the function body.
- There is also a sequence point after the copying of a returned value and before the execution of any expressions outside
the function13). Several contexts in C++ cause evaluation of a function call, even though no corresponding function call
syntax appears in the translation unit. [ Example: evaluation of a new expression invokes one or more allocation and
constructor functions; see 5.3.4. For another example, invocation of a conversion function (12.3.2) can arise in contexts
in which no function call syntax appears. —end example ] The sequence points at function-entry and function-exit
(as described above) are features of the function calls as evaluated, whatever the syntax of the expression that calls the
function might be.
In the evaluation of each of the expressions
a && b
a || b
a ? b : c
a , b
using the built-in meaning of the operators in these expressions (5.14, 5.15, 5.16, 5.18), there is a sequence point after
the evaluation of the first expression14).
I will use the sequence point list from the draft C99 standard
Annex C which although it is not normative I can find no disagreement with the normative sections it references. It says:
The following are the sequence points described in 18.104.22.168:
- The call to a function, after the arguments have been evaluated (22.214.171.124).
- The end of the first operand of the following operators: logical AND && (6.5.13);
logical OR || (6.5.14); conditional ? (6.5.15); comma , (6.5.17).
- The end of a full declarator: declarators (6.7.5);
- The end of a full expression: an initializer (6.7.8); the expression in an expression
statement (6.8.3); the controlling expression of a selection statement (if or switch)
(6.8.4); the controlling expression of a while or do statement (6.8.5); each of the
expressions of a for statement (126.96.36.199); the expression in a return statement
The following entries do not seem to have equivalents in the draft C++ standard but these come from the C standard library which C++ incorporates by reference:
- Immediately before a library function returns (7.1.4).
- After the actions associated with each formatted input/output function conversion
specifier (7.19.6, 7.24.2).
- Immediately before and immediately after each call to a comparison function, and
also between any call to a comparison function and any movement of the objects
passed as arguments to that call (7.20.5).
So there is not much of a difference between C and C++ here.
When it comes to the typical examples of sequence points and undefined behavior, for example those covered in Section
5 Expression dealing with modifying a variable more than once within a sequence points I can not come up with an example that is undefined in one but not the other. In C99 it says:
Between the previous and next sequence point an object shall have its
stored value modified at most once by the evaluation of an
expression.72) Furthermore, the prior value shall be read only to
determine the value to be stored.73)
and it provides these examples:
i = ++i + 1;
a[i++] = i;
and in C++ it says:
Except where noted, the order of evaluation of operands of individual
operators and subexpressions of individual expressions, and the order
in which side effects take place, is unspecified.57) Between the
previous and next sequence point a scalar object shall have its stored
value modified at most once by the evaluation of an expression.
Furthermore, the prior value shall be accessed only to determine the
value to be stored. The requirements of this paragraph shall be met
for each allowable ordering of the subexpressions of a full
expression; otherwise the behavior is undefined
and provides these examples:
i = v[i ++]; / / the behavior is undefined
i = ++ i + 1; / / the behavior is undefined
In C++11 and C11 we do have one major difference which is covered in Assignment operator sequencing in C11 expressions which is the following:
i = ++i + 1;
This is due to the result of pre-increment being an lvalue in C++11 but not in C11 even though the sequencing rules are the same.
We do have major difference in areas that have nothing to do with sequence points:
- In C what uses of an indeterminate value is undefined has always been well specified while in C++ it was not until the recent draft C++1y standard that it has been well specified. This is covered in my answer to Has C++ standard changed with respect to the use of indeterminate values and undefined behavior in C++1y?
- Type punning through a union has always been well defined in C but not in C++ or at least it is hotly debatable whether it is undefined behavior or not. I have several references to this in my answer to Why does optimisation kill this function?
- In C++ simply falling off the end of value returning function is undefined behavior while in C it is only undefined behavior if you use the value.
There are probably plenty more examples but these are ones I have written about before.
Sequence points and side effects in C
Finally I got an explanation on SO about this point . After reading it and FAQ I concluded that;
1.The last sentence
Furthermore, the prior value shall be accessed only to determine the value to be stored
would be like this;
Furthermore, the prior value of an object shall be accessed only to determine the modified/new value( of same object ) to be stored.
As it is clear by the example
int i = 1, j, a;
i = i + 1;
j = i + 1;
a[i] = i;
in case of expression
i = i + 1 prior value (which is
1 here) of
i (in R.H.S) is accessed to determine the value of
i to be stored and this is what the statement
if an object is written to within a full expression, any and all accesses to it within the same expression must be directly involved in the computation of the value to be written
While in case of
j = i + 1 and
a[i] = i, the accessed value of
i is just value not prior value as no where
i is modified in these statements.
2.The second question can be explained as;
In case of expression
a[i] = i++ or
a[i++] = i, first sentence of above statement
Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression.
get failed as
i is modified only once between two consecutive sequence point. And that's why we need second sentence.
Both of these examples are disallowed in C because the prior value of
i accessed two times i.e,
i++ itself access prior value of
i in the expression to modify it and hence other access of prior value / value of
i is needless as it is not accessed to determine the modified value to be stored.
Does this C code result in Undefined Behavior?
There is a gigantic difference between the expressions
b++ + ++c - --d - e--
(which is fine), and
x++ + ++x - --x - x--
(which is rampantly undefined).
It's not using
-- that makes an expression undefined. It's not even using
-- twice in the same expression. No, the problem is when you use
-- to modify a variable inside an expression, and you also try to use the value of that same variable elsewhere in the same expression, and without an intervening sequence point.
Consider the simpler expression
++z + z;
Now, obviously the subexpression
++z will increment
z. So the question is, does the
+ z part use the old or the new value of z? And the answer is that there is no answer, which is why this expression is undefined.
Remember, expressions like
++z do not just mean, "take
z's value and add 1". They mean, "take
z's value and add 1, and store the result back into
z". These expressions have side effects. And the side effects are at the root of the undefinedness issue.
Side effects, sequence points and undefined behaviour
?: operator introduces a sequence point, so there's no undefined behavior here.
(i++) > (j) is evaluated and the side effect of
i++ is applied. If the result of
(i++) > (j) is true, then
(i++) is evaluated again, otherwise
(j) is evaluated again.
i++ evalutes to the value of
i before the increment. So, assuming
i > j, then after evaluating
n = i++ > j ? i++ : j;
the following should be true:
n = iorig + 1
i = iorig + 2
Chapter and verse
6.5.15 Conditional operator
4 The first operand is evaluated; there is a sequence point between its evaluation and the
evaluation of the second or third operand (whichever is evaluated). The second operand
is evaluated only if the first compares unequal to 0; the third operand is evaluated only if
the first compares equal to 0; the result is the value of the second or third operand
(whichever is evaluated), converted to the type described below.110)
110) A conditional expression does not yield an lvalue.
Undefined Reference to Static Class Member
What Are Copy Elision and Return Value Optimization
What Does the C++ Standard State the Size of Int, Long Type to Be
Regular Cast Vs. Static_Cast Vs. Dynamic_Cast
How to Parse a String to an Int in C++
How to Add "-L" (Ell) Compiler Flag in Cmake
What Are Some Uses of Template Template Parameters
What Is the Lifetime of a Static Variable in a C++ Function
How to Generate a Random Number in C++
Visual Studio Code: Take Input from User
Resolve Build Errors Due to Circular Dependency Amongst Classes
Is Short-Circuiting Logical Operators Mandated? and Evaluation Order
Cout ≪≪ With Char* Argument Prints String, Not Pointer Value
Why Does Reading a Record Struct Fields from Std::Istream Fail, and How to Fix It
What Is "Rvalue Reference For *This"
Why Can't the Switch Statement Be Applied on Strings