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 7 of 12 Topic 45818 of 48020
Post > Topic >>

Re: Unpredictable nature of increment operators

by Jerry Coffin <jcoffin@[EMAIL PROTECTED] > May 10, 2008 at 09:01 AM

In article <d16e4f6a-c4f2-4b6f-bf7d-
ea1258da781c@[EMAIL PROTECTED]
>, james.kanze@[EMAIL PROTECTED]
 
says...
> On 9 mai, 19:21, "Victor Bazarov" <v.Abaza...@[EMAIL PROTECTED]
> wrote:

[ ... ]

> > For example, it's not undefined behaviour to do
> 
> >     f(i++) + ++i;
> 
> Yes it is, since there is no sequence point between the two
> incrementations.  Sequence points only define a partial
> ordering: there is a sequence point before calling f, but that
> only establishes and ordering between i++ and the call to f; it
> doesn't establish any ordering between the two incrementations.

I believe it establishes _some_ ordering, but not enough[1]. In 
particular, I believe the compiler is required to treat evaluation of a 
function argument and calling the function as atomic -- i.e. once the 
evaluation of any argument takes place, it must proceed to evaluated the 
other arguments (if any) and then call the function.

In the expression above, I don't believe it's allowed for the post- 
increment to be evaluated, then the pre-increment, then the function 
call. It is, however, allowed for the pre-increment, then the post-
increment, then the function call -- and in this ordering, there is no 
sequence point between the pre-increment and the post-increment, so the 
result is undefined behavior.

1: Though it's open to some question. $1.9/8 says: "Once the execution 
of a function begins, no expressions from the calling function are 
evaluated until execution of the called function has completed."

I'm interpreting evaluating the arguments to a function as part of 
execution of the function. If you choose to interpret it as a completely 
separate act that happens before the function's execution, then you're 
right -- no ordering is defined. At least in this case, it doesn't make 
any real difference though -- the overall result is undefined behavior 
either way.

-- 
    Later,
    Jerry.

The universe is a figment of its own imagination.
 




 12 Posts in Topic:
Unpredictable nature of increment operators
bintom <binoythomas110  2008-05-08 17:16:56 
Re: Unpredictable nature of increment operators
Ian Collins <ian-news@  2008-05-09 12:17:58 
Re: Unpredictable nature of increment operators
Martin York <Martin.Yo  2008-05-09 10:11:08 
Re: Unpredictable nature of increment operators
"Victor Bazarov"  2008-05-09 13:21:26 
Re: Unpredictable nature of increment operators
Jerry Coffin <jcoffin@  2008-05-10 08:47:59 
Re: Unpredictable nature of increment operators
James Kanze <james.kan  2008-05-10 07:08:42 
Re: Unpredictable nature of increment operators
Jerry Coffin <jcoffin@  2008-05-10 09:01:10 
Re: Unpredictable nature of increment operators
"Bo Persson" &l  2008-05-11 10:56:59 
Re: Unpredictable nature of increment operators
Jerry Coffin <jcoffin@  2008-05-11 09:05:09 
Re: Unpredictable nature of increment operators
Old Wolf <oldwolf@[EMA  2008-05-11 02:14:44 
Re: Unpredictable nature of increment operators
James Kanze <james.kan  2008-05-11 09:09:34 
Re: Unpredictable nature of increment operators
James Kanze <james.kan  2008-05-11 09:19:28 

Post A Reply:
  Go here to Signup

AddThis Feed Button


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

Contact
tan12V112 Tue Oct 14 8:41:12 CDT 2008.