<< question

Discussion in 'C++' started by Ralf Goertz, Aug 17, 2008.

  1. Ralf Goertz

    Ralf Goertz Guest

    (second repost, there seem to be problems)

    Hi,

    this might be g++-related but it raises a general question.

    #include <iostream>

    using namespace std;


    bool getValue(int& val){
    val=int(42);
    return true;
    }

    int main(){
    int i(0);
    cout<< i<<" "<<getValue(i)<<" value "<<i<<endl; // (*)
    }


    Compiled with optimization (-O3) this yields the output

    > 42 1 value 42


    Without optimization it gives

    > 42 1 value 0


    I had expected

    > 0 1 value 42


    Do I have to read the line (*) from right to left? But that seems odd
    since "endl" is at the end of the output.

    Ralf
    Ralf Goertz, Aug 17, 2008
    #1
    1. Advertising

  2. Ralf Goertz

    Bo Persson Guest

    Ralf Goertz wrote:
    > (second repost, there seem to be problems)
    >
    > Hi,
    >
    > this might be g++-related but it raises a general question.
    >
    > #include <iostream>
    >
    > using namespace std;
    >
    >
    > bool getValue(int& val){
    > val=int(42);
    > return true;
    > }
    >
    > int main(){
    > int i(0);
    > cout<< i<<" "<<getValue(i)<<" value "<<i<<endl; // (*)
    > }
    >
    >
    > Compiled with optimization (-O3) this yields the output
    >
    >> 42 1 value 42

    >
    > Without optimization it gives
    >
    >> 42 1 value 0

    >
    > I had expected
    >
    >> 0 1 value 42

    >
    > Do I have to read the line (*) from right to left? But that seems
    > odd since "endl" is at the end of the output.
    >


    You don't know from which end you have to read it - perhaps from the
    middle out?

    The language just doesn't say that the parameters have to be evaluated
    in any specific order. Like you have seen, the compiler can make
    different choices for different compiles.


    Bo Persson
    Bo Persson, Aug 17, 2008
    #2
    1. Advertising

  3. Ralf Goertz wrote:
    > bool getValue(int& val){
    > val=int(42);
    > return true;
    > }
    >
    > int main(){
    > int i(0);
    > cout<< i<<" "<<getValue(i)<<" value "<<i<<endl; // (*)


    Because your getValue() function has side effects, the results of that
    last expression are undefined because there's more than one reference to
    the value being modified in the same expression. This is because the
    compiler is allowed to optimize that kind of expression in any way it
    wants. The result is only guaranteed after the ';'.

    If you want it to work "correctly", divide each reference to 'i' into
    its own expression:

    cout << i << " ";
    cout << getValue(i);
    cout << " value " << i << endl;
    Juha Nieminen, Aug 17, 2008
    #3
  4. Ralf Goertz

    Fraser Ross Guest

    "Bo Persson"
    > > int main(){
    > > int i(0);
    > > cout<< i<<" "<<getValue(i)<<" value "<<i<<endl; // (*)
    > > }

    > The language just doesn't say that the parameters have to be evaluated
    > in any specific order. Like you have seen, the compiler can make
    > different choices for different compiles.


    If you are meaning expressions when you said parameters then thats true.
    If you meant the 2 function parameters of operator<< then that is
    irrelevant.

    Fraser.
    Fraser Ross, Aug 17, 2008
    #4
  5. Ralf Goertz

    Salt_Peter Guest

    On Aug 17, 4:32 am, Ralf Goertz
    <> wrote:
    > (second repost, there seem to be problems)
    >
    > Hi,
    >
    > this might be g++-related but it raises a general question.
    >
    > #include <iostream>
    >
    > using namespace std;
    >
    > bool getValue(int& val){
    > val=int(42);
    > return true;
    >
    > }
    >
    > int main(){
    > int i(0);
    > cout<< i<<" "<<getValue(i)<<" value "<<i<<endl; // (*)
    >
    > }
    >
    > Compiled with optimization (-O3) this yields the output
    >
    > > 42 1 value 42

    >
    > Without optimization it gives
    >
    > > 42 1 value 0

    >
    > I had expected
    >
    > > 0 1 value 42

    >
    > Do I have to read the line (*) from right to left? But that seems odd
    > since "endl" is at the end of the output.
    >
    > Ralf



    You can read it upside down or any way you like. The language doesn't
    specify the order of evaluation. Thats the programmer's
    responsability. What result you get on this compiler may not match
    what another might do or what another version might do.

    Its up to you to supply the (required) sequence points.
    Salt_Peter, Aug 17, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sean
    Replies:
    1
    Views:
    568
    Cowboy \(Gregory A. Beamer\)
    Oct 20, 2003
  2. =?Utf-8?B?UnlhbiBTbWl0aA==?=

    Quick Question - Newby Question

    =?Utf-8?B?UnlhbiBTbWl0aA==?=, Feb 14, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    627
    Iain Norman
    Feb 16, 2005
  3. =?Utf-8?B?YW5kcmV3MDA3?=

    question row filter (more of sql query question)

    =?Utf-8?B?YW5kcmV3MDA3?=, Oct 5, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    923
    Scott Allen
    Oct 6, 2005
  4. Philip Meyer
    Replies:
    0
    Views:
    401
    Philip Meyer
    Nov 30, 2003
  5. Bit Byte
    Replies:
    1
    Views:
    813
    Teemu Keiski
    Jan 28, 2007
Loading...

Share This Page