C++ Expression Evaluation

In C++, Programming by timfanelliLeave a Comment

I recently posted a link to Bjorne Stroustrup’s C++ FAQ, in which he points out that the value of:

int i = 2
int j = i++ + i++

is undefined. I decided to throw together a couple little sample problems to see just what it would do. My toy program looks like this:

#include <iostream>

using namespace std;

int main( int argc, char ** argv ) {
	int i = 2;
	int j = <expr1> + <expr2>;

	cout << j << endl;

Where expr1 and expr2 are either i++ or ++i. So for instance, when I throw in

int j = i++ + i++

I would expect j to take on a value of 5 — since i is initially 2, the first i++ would evaluate to 2 and assign a value of 3 to i. The second i++ then evaluates to 3 and assigns a value of 4 to i. The results were, as expected, not what I would expect. The following table summarizes the combination of i++’s and ++i’s, what I would evaluate it to in my head, and what actually happened:

Expr 1Expr 2Expected jActual ji

As you can see, when we mix ++i and i++, things seem to evaluate as expected, however i++ + i++ and ++i + ++i are incorrect. As B.S. points out in his FAQ, this behavior is due to the fact that the order of evaluation of expressions is undefined. So when we do i++ + i++, there is no garuantee that expressions are evaluated left to right, and there is no garuantee that the ++ operators will be evaluated before the addition operation (even though, interestingly enough, ++ has a higher precendence than +)

Bjarne’s answer to this nasty “problem”? Just don’t do it. Expressions like this are confusing anyway, and simple enough to avoid.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.