Connecting Tech Pros Worldwide Help | Site Map

Sequence points.

 
LinkBack Thread Tools Search this Thread
  #1  
Old May 26th, 2006, 08:15 AM
kwrzalik@gmail.com
Guest
 
Posts: n/a
Default Sequence points.

Hello,
I was solving a C++ quiz that has the following question:


What is the outcome of running the following code:

int c=0;
cout<<c++<<c;

The answer says undefined. But in this case, isn't this statement
translated into:

cout.operator<<(c++).operator<<(c);

There should be a sequnce point before calling a funcion after
evaluation of the arguments. So shouldn't the incremented value of c be
stored before the call to first <<. If so it should be available for
the second <<, and the result would be 01.

Please help me with this confusion.


  #2  
Old May 26th, 2006, 09:03 AM
benben
Guest
 
Posts: n/a
Default Re: Sequence points.

kwrzalik@gmail.com wrote:[color=blue]
> Hello,
> I was solving a C++ quiz that has the following question:
>
>
> What is the outcome of running the following code:
>
> int c=0;
> cout<<c++<<c;
>
> The answer says undefined. But in this case, isn't this statement
> translated into:
>
> cout.operator<<(c++).operator<<(c);
>
> There should be a sequnce point before calling a funcion after
> evaluation of the arguments. So shouldn't the incremented value of c be
> stored before the call to first <<. If so it should be available for
> the second <<, and the result would be 01.
>
> Please help me with this confusion.
>[/color]

Compilers have right to delay the increment operation on c until the end
of the statement.

Ben
  #3  
Old May 26th, 2006, 09:03 AM
Jonathan Mcdougall
Guest
 
Posts: n/a
Default Re: Sequence points.

kwrza...@gmail.com wrote:[color=blue]
> Hello,
> I was solving a C++ quiz that has the following question:
>
>
> What is the outcome of running the following code:
>
> int c=0;
> cout<<c++<<c;
>
> The answer says undefined. But in this case, isn't this statement
> translated into:
>
> cout.operator<<(c++).operator<<(c);
>
> There should be a sequnce point before calling a funcion after
> evaluation of the arguments. So shouldn't the incremented value of c be
> stored before the call to first <<. If so it should be available for
> the second <<, and the result would be 01.[/color]

There is a sequence point between the function calls, but not between
the evaluation of the arguments. That means, whether "c" is evaluated
before or after "c++" is undefined. Don't do that.


Jonathan

  #4  
Old May 26th, 2006, 09:45 AM
kwrzalik@gmail.com
Guest
 
Posts: n/a
Default Re: Sequence points.

Could you please elaborate on this more verbosely? I understand that if
c++ is evaluated as an argument of a function and there's a sequence
point bofore calling this function the increment operator must be
executed and saved at this point. Because the dot operator bind from
left to right, the c should be incremented and saved so that it is
accessible in the second operarator<< call.

  #5  
Old May 26th, 2006, 09:45 AM
kwrzalik@gmail.com
Guest
 
Posts: n/a
Default Re: Sequence points.

Are you sure it's the end of the function and not the so called
sequence point?

  #6  
Old May 26th, 2006, 10:15 AM
Rolf Magnus
Guest
 
Posts: n/a
Default Re: Sequence points.

kwrzalik@gmail.com wrote:
[color=blue]
> Could you please elaborate on this more verbosely? I understand that if
> c++ is evaluated as an argument of a function and there's a sequence
> point bofore calling this function the increment operator must be
> executed and saved at this point.[/color]

Yes, but the other one can be executed already, too. It doesn't have to be
deferred to after the first function (or even after operator++) is called.
The compiler is free to first evaluate c, then c++, then call the first
operator<<, then the second one.
At least that's how I understand it.

  #7  
Old May 26th, 2006, 11:15 AM
benben
Guest
 
Posts: n/a
Default Re: Sequence points.

kwrzalik@gmail.com wrote:[color=blue]
> Are you sure it's the end of the function and not the so called
> sequence point?
>[/color]

Negative...I'll go back and read the standard...

Regards,
Ben
  #8  
Old May 26th, 2006, 12:25 PM
Kyku
Guest
 
Posts: n/a
Default Re: Sequence points.

You seem to be right. Thank you for your explanation. I didn't think
about it that the two c's can be evaluated at any point.

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.