order of evaluation

Discussion in 'C++' started by ASobol, Feb 15, 2005.

  1. ASobol

    ASobol Guest

    //code.begin()
    #include <iostream>

    int g (int i)
    {
    static const int j = i;
    return j;
    }

    int main (int, char**)
    {
    std::cout << g(0) << g(1) << g (2) << g(3);
    return 0;
    }
    //code.end()

    Is output of this program undefined due to C++ standart?
    Compilers i have access to (msvc 7.1 and gcc 3.3) both produce "3333"
    output.


    --Sasha
     
    ASobol, Feb 15, 2005
    #1
    1. Advertising

  2. ASobol

    Sumit Rajan Guest

    "ASobol" <> wrote in message
    news:...
    > //code.begin()
    > #include <iostream>
    >
    > int g (int i)
    > {
    > static const int j = i;
    > return j;
    > }
    >
    > int main (int, char**)
    > {
    > std::cout << g(0) << g(1) << g (2) << g(3);


    The compiler is allowed to determine the order in which the 4 calls to g()
    are executed.

    > return 0;
    > }


    > //code.end()
    >
    > Is output of this program undefined due to C++ standart?


    Undefined behaviour. See:
    http://www.parashift.com/c -faq-lite/misc-technical-issues.html#faq-38.16
    http://www.parashift.com/c -faq-lite/misc-technical-issues.html#faq-38.15
    http://www.langer.camelot.de/Articles/VSJ/SequencePoints/SequencePoints.html



    Regards,
    Sumit.
    --
    Sumit Rajan <>
     
    Sumit Rajan, Feb 15, 2005
    #2
    1. Advertising

  3. ASobol

    msalters Guest

    Sumit Rajan wrote:
    > "ASobol" <> wrote in message
    > news:...
    > > //code.begin()
    > > #include <iostream>
    > >
    > > int g (int i)
    > > {
    > > static const int j = i;
    > > return j;
    > > }
    > >
    > > int main (int, char**)
    > > {
    > > std::cout << g(0) << g(1) << g (2) << g(3);

    >
    > The compiler is allowed to determine the order in which the 4 calls

    to g()
    > are executed.


    Yes, but it may not execute them concurrently (unless you cannot detect
    it, of course. "as if"-rule ). One call must be made first. It may be a
    different choice on every run, though.

    > > Is output of this program undefined due to C++ standart?

    >
    > Undefined behaviour.


    Wrong, unspecified. There are proper sequence points between the
    calls, the only thing unspecified is the order of sequence points.
    No variable is modified twice between sequence points. In fact,
    the only variable modified is static const int j, which by
    definition cannot be modified twice at all.

    Regards,
    Michiel Salters
     
    msalters, Feb 15, 2005
    #3
  4. ASobol

    Sumit Rajan Guest

    "msalters" <> wrote in message
    news:...
    >> Undefined behaviour.

    >
    > Wrong, unspecified.


    You're right. Thanks for pointing that out.

    Regards,
    Sumit.
    --
    Sumit Rajan <>
     
    Sumit Rajan, Feb 15, 2005
    #4
    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. Nan Li

    evaluation order

    Nan Li, Nov 14, 2005, in forum: Java
    Replies:
    11
    Views:
    658
    Oliver Wong
    Nov 15, 2005
  2. Xavier Decoret

    Evaluation order for a=b

    Xavier Decoret, Jul 3, 2003, in forum: C++
    Replies:
    1
    Views:
    347
    Ron Natalie
    Jul 3, 2003
  3. Ilias Lazaridis
    Replies:
    2
    Views:
    397
    Ilias Lazaridis
    Apr 24, 2005
  4. Ilias Lazaridis
    Replies:
    74
    Views:
    780
    Ilias Lazaridis
    Apr 4, 2005
  5. Ilias Lazaridis
    Replies:
    18
    Views:
    355
    Bill Guindon
    Apr 9, 2005
Loading...

Share This Page