2

Possible Duplicate:
Undefined Behavior and Sequence Points

Is there any one know that whether this is valid or not in C++

int a = 0;
a = a++;

Someone told me that it will generate unknown behavior under C++ standard, did anyone know why, and where in the C++ standard states that? Thanks!

Community
  • 1
  • 1
NachoChip
  • 297
  • 4
  • 13
  • 1
    It's already been noted that the behavior is undefined. But whatever you're trying to do, there's certainly a better and clearer way to do it. In this case, a simple `a++;` does what you want with no ambiguity or risk of undefined behavior. – Keith Thompson Oct 19 '11 at 18:44

3 Answers3

3

I've posted it before, and I will post it again:

http://www.slideshare.net/olvemaudal/deep-c

highly recommended for anybody with such questions in mind

Flexo
  • 87,323
  • 22
  • 191
  • 272
emesx
  • 12,555
  • 10
  • 58
  • 91
1

The techincal reason why is that you should not modify the same variable twice (either directly or due to side effects) between sequence points.

Here is an SO question with good answers that clarifies this further and describes sequence points in general.

Community
  • 1
  • 1
Michael Goldshteyn
  • 71,784
  • 24
  • 131
  • 181
0

I don't know about the standard per se (its probably referenced from the C standard anyway), but here you can read about it:

http://www.research.att.com/~bs/bs_faq2.html#evaluation-order

K-ballo
  • 80,396
  • 20
  • 159
  • 169