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 14 of 14 Topic 45817 of 46738
Post > Topic >>

Re: Unpredictable result of Increment operation

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

On 10 mai, 15:47, "Victor Bazarov" <v.Abaza...@[EMAIL PROTECTED]
> wrote:

> A sequence point is a definite *moment* in the execution of the
> program, at which all the side effects of the previous calculations
> have taken place.  There are several situations in which a sequence
> point (SP) is known to exist.  First, there is a SP at a semicolon.
> There is a SP after evaluation of all arguments of a function and
> just before the call to that function.  There is another SP right
> after the function returns.  There is another SP after each object
> is initialised in a definition statement.  A comma operator also
> introduces a SP, and so does every logical OR and logical AND
> operator.  I think that's about it.  I don't think there are other
> sequence points defined.

In 'a ? b : c', between the evaluation of a and either b or c.

> The concept of a sequence point is im****tant for allowing the C++
> compiler to create certain optimizations when it generates code
> from a C++ program.  Sequence points are a limitation on the
> execution of the program, so too many of those would mean the
> code cannot be optimized.  Too few of them might mean the code
> won't behave predictably.

It's also im****tant to realize that the compiler can reorder
code in an expression significantly, and if any of the legal
reorderings violate the rules, you have undefined behavior.  The
classical example is something like:
    f( ++ i ) + g( ++ i ) ;
Undefined behavior, since the compiler can legally reorder this
to be as:
    tmp1 =3D ++ i
    tmp2 =3D ++ i
    f( tmp1 )
    g( tmp2 )
(without any sequence points other than the function calls).

In practice, I (and I suspect that I'm not alone) find it almost
impossible to be 100% sure whether there is a sequence point or
not in some of the more subtle cases.  So the general rule is to
just avoid modifying the same variable more than once in an
expression, or modifying it and using it elsewhere in the
expression.  About the only exceptions I'll allow are for top
level (not in any parentheses) &&, || and ?:.

--
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 Wed Jul 9 0:30:31 CDT 2008.