5

Possible Duplicate:
Undefined Behavior and Sequence Points

I'm not sure if this is a gcc bug or not, so I'll ask:

unsigned int n = 0; std::cout << n++ << n << ++n; 

gcc gives the extremely strange result: "122" which AFAICT is impossible. Because << is left associative, it should be the same as:

operator<<(operator<<(operator<<(std::cout, n++), n), ++n) 

and because there is a sequence point before and after evaluating arguments, n is never modified twice (or even accessed) between two sequence points -- so it shouldn't be undefined behaviour, just the order of evaluation unspecified.

So AFAICT valid results would be: 111 012 002 101

and nothing else

3
  • Not a compiler bug; contrary to your expectation, this is undefined behavior in C++. Commented Mar 7, 2011 at 0:56
  • @ephemient: Unspecified, no? Commented Mar 7, 2011 at 0:59
  • @Oli: ephemient is correct, it's undefined behavior. 1.9p15 (quoted in my answer) Commented Mar 7, 2011 at 1:14

3 Answers 3

10

There is a sequence point between evaluating arguments and calling a function. There is no sequence point between evaluating different arguments.

Let's look at the outermost function call:

operator<<(operator<<(operator<<(std::cout, n++), n), ++n) 

The arguments are

  • operator<<(operator<<(std::cout, n++), n)

and

  • ++n

It is unspecified which of these is evaluated first. It's also allowed that the first argument is partially evaluated when the second argument is evaluated.

From the standard, section [intro.execution] (wording from draft 3225):

  • If A is not sequenced before B and B is not sequenced before A, then A and B are unsequenced. [ Note: The execution of unsequenced evaluations can overlap. — end note ]

  • Except where noted, evaluations of operands of individual operators and of subexpressions of individual expressions are unsequenced. [ Note: In an expression that is evaluated more than once during the execution of a program, unsequenced and indeterminately sequenced evaluations of its subexpressions need not be performed consistently in different evaluations. — end note ] The value computations of the operands of an operator are sequenced before the value computation of the result of the operator. 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.

Because you have multiple operations with side effects on the same scalar object which are unsequenced with respect to each other, you're in that realm of undefined behavior, and even 999 would be a permissible output.

Sign up to request clarification or add additional context in comments.

9 Comments

Thanks Ben, this seems to be the answer! I was under the impression there was a sequence point between evaluating different arguments, but there doesn't appear to be. Also thanks for the non condescending manner :)
@user: Nope... the comma operator sequences its operands, but the comma used between function arguments is not the comma operator.
I would have expected that operations within an expression that includes a function call would be indeterminately sequenced with regard to that call, rather than unsequenced. Certainly if an expression contains two function calls they are indeterminately sequenced, rather than unsequenced, relative to each other.
@supercat: they are indeterminately sequenced wrt the function call, but unsequenced wrt each other.
@BenVoigt: I gotcha. You state that the first argument may be partially evaluated at the time the second argument is evaluated, but I think what you really mean is that the first argument may be partially evaluated when the arguments to the inner function call are evaluated. The function call itself--a key part of the argument evaluation--would be required to occur either entirely before or entirely after the first increment operation, would it not?
|
6

The first rule of compiler bugs: it's probably not a compiler bug but a misunderstanding on your part. Using the postfix and prefix operators in the same statement results in undefined behavior. Try using the -Wall option to give you more warnings and show you the potential pitfalls in your code.

Let's see what GCC 4.2.1 tells us when we ask for warnings about test.cpp:

#include <iostream> int main() { unsigned int n = 0; std::cout << n++ << n << ++n << std::endl; return 0; } 

When we compile:

$ g++ -Wall test.cpp -o test test.cpp: In function ‘int main()’: test.cpp:5: warning: operation on ‘n’ may be undefined 

5 Comments

That rule has a few exceptions, for example when one is working with tools from a certain major Japanese electronics conglomerate. =P
In this case, it's not undefined behaviour, merely unspecified. This isn't the same as the standard i = i + ++i type questions...
@Oli: Is the difference that "unspecified" will be reproducible given the same compiler and platform, whereas "undefined" may change from one program run to the next? Whatever the semantic difference, GCC reports it as "undefined," even though I agree that demons did not come flying out of my nose.
I agree that in practice, neither is a good thing to rely on. However, the standard appears to like make a song and dance about the distinction! I'm not sure why, though.
@Oli: It is undefined behavior, according to draft n3225.
-3

Your code its an example of why in some books remark that experienced programmers don't like that(++,--) operator overload, even other languages (ruby) has not implemented ++ or --.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.