Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ > Re: Unpredictab...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 13 of 14 Topic 45817 of 47032
Post > Topic >>

Re: Unpredictable result of Increment operation

by James Kanze <james.kanze@[EMAIL PROTECTED] > May 10, 2008 at 08:30 AM

On 10 mai, 10:34, brno <brno.barutc...@[EMAIL PROTECTED]
> wrote:
> James Kanze dixit:

> > On May 9, 2:54 am, "Victor Bazarov" <v.Abaza...@[EMAIL PROTECTED]
> wrote:
> >> Ian Collins wrote:
> >>> bintom wrote:
> >>>> Is there any reason why the following C++ code behaves as it does ?

> >>>> int i;

> >>>> i=3D1;  cout << (++i)++;           Output: 2

> >>> Because it's nonsensical undefined behaviour.

> >> Why is it non-sensical?  Why is it undefined?

> > Because the standard says so.  You're modifying the same object
> > twice without an intervening sequence point.

> Can you tell what is an intervening sequence point ?

A sequence point that is guaranteed to occur between the two
operations.

In your example, the only sequence points are the ends of each
full expression, and the call and return from the
ostream::operator<<( int ) function.  There's nothing between
the ++i (which modifies i), and the (x)++ (where 'x' is the
result of ++i, i.e. i), which also modifies i.

Note that if i was a type with a user defined operator++, the
code would be well defined, since the user defined operator++ is
a function, and both the function call and the return are
sequence points.  Note too, however, that sequence points do not
always create a complete ordering: in something like 'f(++i) +
g(++i)', there are still no sequence points between the two
incrementations, so the code has undefined behavior.

--
James Kanze (GABI Software)             email:james.kanze@[EMAIL PROTECTED]
 en informatique orient=E9e objet/
                   Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34
 




 14 Posts in Topic:
Unpredictable result of Increment operation
bintom <binoythomas110  2008-05-08 17:12:15 
Re: Unpredictable result of Increment operation
Ian Collins <ian-news@  2008-05-09 12:16:10 
Re: Unpredictable result of Increment operation
"Victor Bazarov"  2008-05-08 20:54:12 
Re: Unpredictable result of Increment operation
Ian Collins <ian-news@  2008-05-09 13:31:15 
Re: Unpredictable result of Increment operation
Ian Collins <ian-news@  2008-05-10 20:55:51 
Re: Unpredictable result of Increment operation
Jack Klein <jackklein@  2008-05-08 21:46:21 
Re: Unpredictable result of Increment operation
bintom <binoythomas110  2008-05-09 01:02:00 
Re: Unpredictable result of Increment operation
James Kanze <james.kan  2008-05-09 06:10:12 
Re: Unpredictable result of Increment operation
"Victor Bazarov"  2008-05-09 10:18:03 
Re: Unpredictable result of Increment operation
brno <brno.barutchek@[  2008-05-10 10:34:32 
Re: Unpredictable result of Increment operation
Ian Collins <ian-news@  2008-05-10 21:01:24 
Re: Unpredictable result of Increment operation
"Victor Bazarov"  2008-05-10 09:47:14 
Re: Unpredictable result of Increment operation
James Kanze <james.kan  2008-05-10 08:30:56 
Re: Unpredictable result of Increment operation
James Kanze <james.kan  2008-05-10 08:37:20 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Jul 25 21:13:14 CDT 2008.