How C++ Evaluates the Stack

I have a question regarding the following code:

1
2
int t = 5;
printf("\n %d %d %d", t, t--, t);


The result is "4 5 4," but I don't understand why I am getting this result. I know that it has something to do with how C++ evaluates the stack, but I have no clue about how that is done exactly. I would expect a result of "5 5 4," but that is (obviously) wrong. I would expect that result because the first t (reading the function argument list from left to right) is initialized as 5, the second t has a post-decrement operator (so it prints 5 and then decrements), and the third t is now 4 (because it has now been decremented). Can anybody please explain the correct result? Thanks!
Last edited on
There literally is no single correct result. The order in which the arguments are evaluated are not defined. The compiler can do them in any order it likes. I have two compilers to hand and one gives 4 5 4 whilst the other gives 5 5 4.

So long as everything has been evaluated by the next sequence point (which in this case is that semi-colon), it's up to the compiler.

If you're relying on side-effects of one argument to get the other arguments as you expect; don't. This is very common with the post-and pre-increment operators.
Last edited on
The order in which the arguments are evaluated are not defined


Didn't know this. I thought they were evaluated from right to left.
Consult the oracle (C++ standards document) and see what that says about it.
Before someone gets picky, that should read "The order in which the arguments are evaluated in this set of arguments that you see in this code right here" are not defined :)

Here are some words from the standard:


1.9.3 Certain other aspects and operations of the abstract machine are described in this International Standard as unspecified (for example, order of evaluation of arguments to a function).

8.3.6.9 ...The order of evaluation of function arguments is unspecified....

Thanks for the information! Definitely enlightening! (I'm preparing for a C++ test for a job I applied to, so I wanted to make sure I have stuff like this down solid.) I found an interesting link that discusses exactly how the stack operates for a function:

http://www.learncpp.com/cpp-tutorial/79-the-stack-and-the-heap/
skyline01 wrote:
I found an interesting link that discusses exactly how the stack operates for a function:

http://www.learncpp.com/cpp-tutorial/79-the-stack-and-the-heap/


I would take the stack part in the spirit is it given :-)
Last edited on
The order of evaluation of function arguments is unspecified....

This particular case is worse than that. In this particular case, the variable t is a scalar object, and
If a side effect on a scalar object is unsequenced relative to either another side effect on the same scalar object or a value computation using the value of the same scalar object, the behavior is undefined
(quoted from C++11, but older revisions of C++, as well as every revision of C have similar rule)
In this case, t-- invokes a side-effect (change of the value of t), while the two other t's perform value computations (read the value).

Unspecified just means one or the other, some permutation of the order of the arguments will take place, for sure. Undefined means that the program cannot be reasoned about at all: anything is possible.
Last edited on
Topic archived. No new replies allowed.