Jump to content
  • Advertisement
Sign in to follow this  
neverland

i++ and ++i problem

This topic is 4812 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

simple code below: int i = 0; cout << i++ << i++ << i++ << endl; int j = 0; cout << ++j << ++j << ++j << endl; The output is 210 333 on VC2005 beta2 But on gcc 3.3.3 the output is 012 123 I'm a little confused. According to ANSI C++, which is the correct output?

Share this post


Link to post
Share on other sites
Advertisement
Both are correct, it can do either when using multiple ++i and i++'s in a single statement.
It's another one of those places where the standard leaves the ultimate behaviour to the compiler.

Share this post


Link to post
Share on other sites
If you really want details, look into 'sequence points.' They define exactly when you know what has and what has not taken place. Anything between two sequence points is implementation defined, so that the compiler is free to optimize if possible.

CM

Share this post


Link to post
Share on other sites
Yeah, don't do that.

Seriously, people who obfuscate the meaning of their code like that deserve all of the headaches that come with it.

Share this post


Link to post
Share on other sites
Quote:
Original post by Inmate2993
Yeah, don't do that.

Seriously, people who obfuscate the meaning of their code like that deserve all of the headaches that come with it.

Frankly, I don't think it looks obfuscated at all. Intuitively, the gcc output makes the most sense. Top to bottom, left to right. When you're first learning, why would you expect a language to *not* have well defined orders?

CM

Share this post


Link to post
Share on other sites
Quote:
Original post by Conner McCloud
Quote:
Original post by Inmate2993
Yeah, don't do that.

Seriously, people who obfuscate the meaning of their code like that deserve all of the headaches that come with it.

Frankly, I don't think it looks obfuscated at all. Intuitively, the gcc output makes the most sense. Top to bottom, left to right. When you're first learning, why would you expect a language to *not* have well defined orders?

CM


Yes, however any reputable C++ book will cover the fact that the order of evaluation of function parameters is unspecified except that they will be evaluated before the call to the function. None the less, if someone hasn't gotten to that part of the book, it is no wonder they might think such.

Share this post


Link to post
Share on other sites
left to right? I don't know. I'd be pushing them on a stack (maybe through function calls) rather than a queue and evaluating them that way.

That is something my teacher told us about in our C++ class. Hopefully you figured this out before going through hours of headaches.

Share this post


Link to post
Share on other sites
The order of evaluation isn't just implementation defined -- as soon as you alter a value more than one time between sequence points, the expression itself yields undefined behavior, which is actually much worse for you if you are actually using code such as that in your program. In other words,

cout << i++ << i++ << i++ << endl


could literally crash your system, do absolutely nothing, or start playing Doom III; even though in practice, it probably would not do something quite so extreme. So, even if the order of evaluation doesn't matter to you, your code still has completely undefined results when doing something like that which you showed.

Share this post


Link to post
Share on other sites
Quote:
Original post by Polymorphic OOP
could literally crash your system, do absolutely nothing, or start playing Doom III


If that happened it would so kick ass, especially considering that I don't even own Doom III.

Hack, hack, hack, compile, run, doom3, "WTF!"?
Play, play, play...

Share this post


Link to post
Share on other sites
Quote:
Original post by nprz
left to right? I don't know. I'd be pushing them on a stack (maybe through function calls) rather than a queue and evaluating them that way.

cout << i++ << i++ << i++ << endl
=
((((cout << i++) << i++) << i++) << endl)

That's how I looked at it until I was proven wrong. At which point I just decided it was easier just to avoid changing variables in cout statements then to figure out how things actually work. In retrospect, it was a good call [grin]

CM

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!