473,416 Members | 1,630 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

ambiguity of post-increment and post-decrement

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
9 3161
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
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

"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
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
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
"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
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
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
I want to thank everyone for clearing up my errant understanding of
post-increment and post-decrement operators.

-mark
Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Terry Hancock | last post by:
What do you do when a date or time is incompletely specified? ISTM, that as it is, there is no formal way to store this --- you have to guess, and there's no way to indicate that the guess is...
11
by: Alexander Stippler | last post by:
Hi I have already posted and discussed the following problems once, but despite really helpful hints I did not get any further with my problem (I at least learned, first to exactly consider why...
21
by: Paul Steckler | last post by:
Here's some code that's giving me differing results, depending on the compiler. typedef foo { int A,B; } FOO; int main() {
0
by: ciaran.mchale | last post by:
I used Google to find information about JAXB 2.0 and I ended up downloading a document called "The Java Architecture for XML Binding (JAXB) 2.0: Proposed Final Draft September 30, 2005". ...
0
by: Alan T | last post by:
I tried to close the word document object and word application: private Interop.Word.Application WordApp; private Interop.Word.Document WordDoc; // close word and quit word application ...
3
by: subramanian100in | last post by:
I have tried the following in VC++ 2005 Express Edition and g++ in Linux. Consider the class class my_complex { public: my_complex(double r, double i = 10.0) : re(r), im(i) { }...
2
by: subramanian100in | last post by:
Consider the program #include <iostream> using namespace std; void fn(const char * str, char x = 'Y') { cout << "from fn(const char *, char) - " << str << endl; return;
10
by: subramanian100in | last post by:
consider the following program: #include <iostream> using namespace std; class my_complex { public: friend ostream & operator<<(ostream &os, const my_complex &c);
2
by: Fraser Ross | last post by:
struct B1 { void f(); static void f(int); int i; }; struct B2 { void f(double); }; struct I1: B1 { }; struct I2: B1 { };
4
by: abendstund | last post by:
Hi, I have the following code and trouble with ambiguity due to operator overloading.. The code is also at http://paste.nn-d.de/441 snip>>
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.