By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,795 Members | 1,832 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,795 IT Pros & Developers. It's quick & easy.

ambiguity of post-increment and post-decrement

P: n/a
I was reading "Practical C++ Programming" yesterday, and it mentioned
that the order of execution for post-increment and post-decrement
operators was ambiguous.
I had previously learned that a post-increment or post-decrement
operator modifies the operand once the entire statement has been
executed, not during execution of the statement, so this confused me.

An examples given to illustrate the ambiguity is:
a[i] = i++; // may increment i before or after a[i] is evaluated.

I tested with the VC6 compiler and the gnu compiler, but with the same
results: the post-decrement occurs after the entire statement is
executed, not during it's execution.

I have now been told that the actual ANSI C++ Standard is ambiguous
concerning execution order (regardless of what occurs during
compilation with different compiler makers).

I am still in the process of working my way through C++ and I don't
have a copy of the official standard, so I was hoping a more learned
individual would be able to elucidate this troublesome issue for me.
"Pracitcal C++ Programming" recommends against the use of increment
and decrement operators within statements, but I've learned to really
enjoy them.

Thanks for the help
-mark
Jul 22 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Mark Turney wrote:
I was reading "Practical C++ Programming" yesterday, and it mentioned
that the order of execution for post-increment and post-decrement
operators was ambiguous.
I had previously learned that a post-increment or post-decrement
operator modifies the operand once the entire statement has been
executed, not during execution of the statement, so this confused me.

An examples given to illustrate the ambiguity is:
a[i] = i++; // may increment i before or after a[i] is evaluated.

I tested with the VC6 compiler and the gnu compiler, but with the same
results: the post-decrement occurs after the entire statement is
executed, not during it's execution.

I have now been told that the actual ANSI C++ Standard is ambiguous
concerning execution order (regardless of what occurs during
compilation with different compiler makers).
Not quite "ambiguous", more like "undefined". The standard does not
specify what the compiler should do hence you can't depend on a
particular behaviour for code portability.

// this code is 100% portable and likely just as fast
a[i] = i; ++ i;

I am still in the process of working my way through C++ and I don't
have a copy of the official standard, so I was hoping a more learned
individual would be able to elucidate this troublesome issue for me.
"Pracitcal C++ Programming" recommends against the use of increment
and decrement operators within statements, but I've learned to really
enjoy them.


look up references for "sequence points". These are points where all
the side effects should be complete. The ";" at the end of an
expression is usually a good place to look.

Jul 22 '05 #2

P: n/a
Mark Turney wrote:

I was reading "Practical C++ Programming" yesterday, and it mentioned
that the order of execution for post-increment and post-decrement
operators was ambiguous.
I had previously learned that a post-increment or post-decrement
operator modifies the operand once the entire statement has been
executed, not during execution of the statement, so this confused me.

An examples given to illustrate the ambiguity is:
a[i] = i++; // may increment i before or after a[i] is evaluated.

I tested with the VC6 compiler and the gnu compiler, but with the same
results: the post-decrement occurs after the entire statement is
executed, not during it's execution.

I have now been told that the actual ANSI C++ Standard is ambiguous
concerning execution order (regardless of what occurs during
compilation with different compiler makers).
Ambigous is the wrong word. It is simply undefined when the increment
actually happens. All you know is that when the next sequence point
is reached (in the above case this is the end of the statement, the ';')
the increment has happend. But when exactly is undefined.
"Pracitcal C++ Programming" recommends against the use of increment
and decrement operators within statements, but I've learned to really
enjoy them.


There is nothing wrong with them. Your program just must not depend
on some specific execution order, that's all.
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #3

P: n/a

"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message news:3F***************@gascad.at...
Ambigous is the wrong word. It is simply undefined when the increment
actually happens.


It's just simply undefined what happens.
Jul 22 '05 #4

P: n/a
On 10 Dec 2003 09:37:39 -0800, go****@freelance3d.com (Mark Turney)
wrote in comp.lang.c++:
I was reading "Practical C++ Programming" yesterday, and it mentioned
that the order of execution for post-increment and post-decrement
operators was ambiguous.
I had previously learned that a post-increment or post-decrement
operator modifies the operand once the entire statement has been
executed, not during execution of the statement, so this confused me.
You think you learned that, you are wrong.

An examples given to illustrate the ambiguity is:
a[i] = i++; // may increment i before or after a[i] is evaluated.
As others have pointed out, this is just plain undefined behavior.
Within a single expression without a sequence point, you are allowed
to modify an object only once, and access its original value only for
the purpose of deriving the new value.
I have now been told that the actual ANSI C++ Standard is ambiguous
concerning execution order (regardless of what occurs during
compilation with different compiler makers).
The word "ambiguous" is ambiguous in this context. It is not defined
by the C++ standard.
I am still in the process of working my way through C++ and I don't
have a copy of the official standard, so I was hoping a more learned
individual would be able to elucidate this troublesome issue for me.
"Pracitcal C++ Programming" recommends against the use of increment
and decrement operators within statements, but I've learned to really
enjoy them.

Thanks for the help
-mark


--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Jul 22 '05 #5

P: n/a
Karl Heinz Buchegger wrote:

Ambigous is the wrong word. It is simply undefined when the increment


`Ambigous' isn't even a word! Can't you spel?

--ag

[ ;-) ]
--
Artie Gold -- Austin, Texas
Oh, for the good old days of regular old SPAM.

Jul 22 '05 #6

P: n/a
"Mark Turney" <go****@freelance3d.com> wrote:
I had previously learned that a post-increment or post-decrement
operator modifies the operand once the entire statement has been
executed, not during execution of the statement, so this confused me.

An examples given to illustrate the ambiguity is:
a[i] = i++; // may increment i before or after a[i] is evaluated. "Pracitcal C++ Programming" recommends against the use of increment
and decrement operators within statements, but I've learned to really
enjoy them.


I'm not sure if you meant that increment and decrement operators
_themselves_ are bad - there is no problem if "i" is not mentioned twice in
the same statement. The statement:

a[j] = i++;

causes no problems at all ...

Another "undefined" example is in a function call like f(i, i++), but just
f(i++) would be OK.

David F
Jul 22 '05 #7

P: n/a
Hi!

First law of Internet thermodynamics:

"Every flame has an equally strong but opposite counterflame."

For instance, if you post a flame about grammar or spelling,
the probability that your article has at least one grammatic or
spelling error is asymptotically equal to 1 .

Now, let's see if one exists here...

"Artie Gold" <ar*******@austin.rr.com> wrote in message
news:3F**************@austin.rr.com...

`Ambigous' isn't even a word! Can't you spel?


Ah! Science is wonderful!

- Risto -

Jul 22 '05 #8

P: n/a
Risto Lankinen wrote:
Hi!

First law of Internet thermodynamics:

"Every flame has an equally strong but opposite counterflame."

For instance, if you post a flame about grammar or spelling,
the probability that your article has at least one grammatic or
spelling error is asymptotically equal to 1 .

Now, let's see if one exists here...

"Artie Gold" <ar*******@austin.rr.com> wrote in message
news:3F**************@austin.rr.com...
`Ambigous' isn't even a word! Can't you spel?

Ah! Science is wonderful!

- Risto -


Alas, you failed to quote my closing [ ;-) ]...

So there!

[;-)]

--ag

--
Artie Gold -- Austin, Texas
Oh, for the good old days of regular old SPAM.

Jul 22 '05 #9

P: n/a
I want to thank everyone for clearing up my errant understanding of
post-increment and post-decrement operators.

-mark
Jul 22 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.