Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it undefined behaviour if multiple operands in a compound expression modify the same object?

I vaguely remember reading somewhere that it is undefined behaviour if multiple operands in a compound expression modify the same object.

I believe an example of this UB is shown in the code below however I've compiled on g++, clang++ and visual studio and all of them print out the same values and can't seem to produce unpredictable values in different compilers.

#include <iostream>

int a( int& lhs ) { lhs -= 4; return lhs; }
int b( int& lhs ) { lhs *= 7; return lhs; }
int c( int& lhs ) { lhs += 1; return lhs; }
int d( int& lhs ) { lhs += 2; return lhs; }
int e( int& lhs ) { lhs *= 3; return lhs; }

int main( int argc, char **argv )
{
    int i = 100;
    int j = ( b( i ) + c( i ) ) * e( i ) / a( i ) * d( i );

    std::cout << i << ", " << j << std::endl;

    return 0;
}

Is this behaviour undefined or have I somehow conjured up a description of supposed UB that is not actually undefined?

I would be grateful if someone could post an example of this UB and maybe even point me to where in the C++ standard that it says it is UB.

like image 492
ctor Avatar asked Dec 22 '12 17:12

ctor


3 Answers

No. It is not. Undefined behavior is out of question here (assuming the int arithmetic does not overflow): all modifications of i are isolated by sequence points (using C++03 terminology). There's a sequence point at the entrance to each function and there's a sequence point at the exit.

The behavior is unspecified here.

Your code actually follows the same pattern as the classic example often used to illustrate the difference between undefined and unspecified behavior. Consider this

int i = 1;
int j = ++i * ++i;

People will often claim that in this example the "result does not depend on the order of evaluation and therefore j must always be 6". This is an invalid claim, since the behavior is undefined.

However in this example

int inc(int &i) { return ++i; }

int i = 1;
int j = inc(i) * inc(i);

the behavior is formally only unspecified. Namely, the order of evaluation is unspecified. However, since the result of the expression does not depend on the order of evaluation at all, j is guaranteed to always end up as 6. This is an example of how generally dangerous unspecified behavior combination can lead to perfectly defined result.

In your case the result of your expression does critically depend on the order of evaluation, which means that the result will be unpredictable. Yet, there's no undefined behavior here, i.e. the program is not allowed to format your hard drive. It is only allowed to produce unpredictable result in j.

P.S. Again, it might turn out that some of the evaluation scenarios for your expression lead to signed integer overflow (I haven't analyzed them all), which by itself triggers undefined behavior. So, there's still a potential for unspecified behavior leading to undefined behavior in your expression. But this is probably not what your question is about.

like image 107
AnT Avatar answered Oct 23 '22 17:10

AnT


No its not undefined behavior.

But it does invoke unspecified behavior.

This is because the order that sub-expressions are evaluated is unspecified.

int j = ( b( i ) + c( i ) ) * e( i ) / a( i ) * d( i );

In the above expression the sub expressions:

b(i)
c(i)
e(i)
a(i)
d(i)

Can be evaluated in any order. Because they all have side-effects the results will depend on this order.

If you divide up the expression into all sub-expressions (this is pseudo-code)
Then you can see any ordering required. Not only can the above expressions be done in any order, potentially they can be interleaved with the higher level sub-expressions (with only a few constraits).

tmp_1 = b(i)           // A
tmp_2 = c(i)           // B
tmp_3 = e(i)           // C
tmp_4 = a(i)           // D
tmp_5 = d(i)           // E

tmp_6 = tmp_1 + tmp_2  // F   (Happens after A and B)
tmp_7 = tmp_6 * tmp_3  // G   (Happens after C and F)
tmp_8 = tmp_7 / tmp_4  // H   (Happens after D and G)
tmp_9 = tmp_8 * tmp_5  // I   (Happens after E and H)

int j = tmp_9;         // J   (Happens after I)
like image 37
Martin York Avatar answered Oct 23 '22 17:10

Martin York


It isn't undefined behavior but it has unspecified results: The only modified object is i through the references passed to the functions. However, the call to the functions introduce sequence points (I don't have the C++ 2011 with me: they are called something different there), i.e. there is no problem of multiple changes within an expression causing undefined behavior.

However, the order in which the expression is evaluated isn't specified. As a result you may get different results if the order of the evaluation changes. This isn't undefined behavior: The result is one of all possible orders of evaluation. Undefined behavior means that the program can behave in any way it wants, including producing the "expected" (expected by the programmer) results for the expression in question while currupting all other data.

like image 24
Dietmar Kühl Avatar answered Oct 23 '22 17:10

Dietmar Kühl