Ambiguous Expression

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Vladimir

    Ambiguous Expression

    Colleagues,

    Suppose I have (in simplified form, of course)

    struct Vect2D {
    double x, y;
    Vect2D(double x_, double y_) : x(x_), y(y_) {}
    };

    which I have to construct with random numbers:

    Vect2D v(rand(), rand());

    But this is "Ambiguous Expression" - i.e. "...languag e does not
    guarantee the order in which arguments to a function call are
    evaluated."

    I was beaten by such type of code indeed - release and debug builds
    behave differently :(

    Above code is so typical, do I have to force explicit order of argument
    evaluation? It would not be so compact and nice.


    --
    Vladimir

  • Victor Bazarov

    #2
    Re: Ambiguous Expression

    Vladimir wrote:[color=blue]
    > Colleagues,
    >
    > Suppose I have (in simplified form, of course)
    >
    > struct Vect2D {
    > double x, y;
    > Vect2D(double x_, double y_) : x(x_), y(y_) {}
    > };
    >
    > which I have to construct with random numbers:
    >
    > Vect2D v(rand(), rand());
    >
    > But this is "Ambiguous Expression" - i.e. "...languag e does not
    > guarantee the order in which arguments to a function call are
    > evaluated."
    >
    > I was beaten by such type of code indeed - release and debug builds
    > behave differently :(
    >
    > Above code is so typical, do I have to force explicit order of argument
    > evaluation? It would not be so compact and nice.[/color]

    Of course you do. You always have to account for side effects of function
    calls. However, let me note here that one shouldn't really care about the
    order when _random_ values are concerned, should one?

    V

    Comment

    • Vladimir

      #3
      Re: Ambiguous Expression

      Victor Bazarov wrote:[color=blue]
      > However, let me note here that one shouldn't really care about the
      > order when _random_ values are concerned, should one?[/color]

      I thought so too :)
      But the actual problem isn't the order itself. Modern compilers with
      global opimizations and inlining will reach a situation like

      call(++i, ++i);

      which can result in both arguments being the same!
      That's not really random to me :)

      [color=blue][color=green]
      > > Vect2D v(rand(), rand());[/color]
      >
      > Of course you do. You always have to account for side effects of[/color]
      function[color=blue]
      > calls.[/color]

      But C++ is quite powerful in allowing to use stuff on the fly which
      really helps in large programs. What can be most compact, preferably
      one-line solution to this problem then?


      --
      Vladimir

      Comment

      • Victor Bazarov

        #4
        Re: Ambiguous Expression

        "Vladimir" <vladimir347@ya hoo.com> wrote...[color=blue]
        > Victor Bazarov wrote:[color=green]
        >> However, let me note here that one shouldn't really care about the
        >> order when _random_ values are concerned, should one?[/color]
        >
        > I thought so too :)
        > But the actual problem isn't the order itself. Modern compilers with
        > global opimizations and inlining will reach a situation like
        >
        > call(++i, ++i);
        >
        > which can result in both arguments being the same!
        > That's not really random to me :)[/color]

        The expression above has undefined behaviour because the object 'i' has
        its _stored_value_ changed more than once between sequence points.
        [color=blue][color=green][color=darkred]
        >> > Vect2D v(rand(), rand());[/color]
        >>
        >> Of course you do. You always have to account for side effects of[/color]
        > function[color=green]
        >> calls.[/color]
        >
        > But C++ is quite powerful in allowing to use stuff on the fly which
        > really helps in large programs. What can be most compact, preferably
        > one-line solution to this problem then?[/color]

        There isn't any. Use separately declared/defined/initialised objects:

        int r1 = rand(), r2 = rand();
        Vect2D v(r1, r2);

        Victor


        Comment

        • Ioannis Vranos

          #5
          Re: Ambiguous Expression

          Victor Bazarov wrote:
          [color=blue]
          > There isn't any. Use separately declared/defined/initialised objects:
          >
          > int r1 = rand(), r2 = rand();
          > Vect2D v(r1, r2);[/color]


          What about this?


          int x;

          Vect2D v((x=rand(), rand()), x);


          :-)




          --
          Ioannis Vranos


          Comment

          • Ron Natalie

            #6
            Re: Ambiguous Expression

            Ioannis Vranos wrote:
            [color=blue]
            >
            > int x;
            >
            > Vect2D v((x=rand(), rand()), x);
            >[/color]


            That's worse. The second argument to the
            v initializer may be evaluated before the first.

            Comment

            • Vladimir

              #7
              Re: Ambiguous Expression


              Victor Bazarov wrote:[color=blue][color=green]
              > > call(++i, ++i);
              > >
              > > which can result in both arguments being the same!
              > > That's not really random to me :)[/color]
              >
              > The expression above has undefined behaviour because the object 'i'[/color]
              has[color=blue]
              > its _stored_value_ changed more than once between sequence points.[/color]

              Similar situation is with rand() - after inlining, internal value
              (used to hold random state) is changed more than once - that's why
              the problem occurs.

              [color=blue]
              > There isn't any. Use separately declared/defined/initialised[/color]
              objects:[color=blue]
              >
              > int r1 = rand(), r2 = rand();
              > Vect2D v(r1, r2);[/color]

              I wish we could always remember to use this when needed.
              What about encapsulating such functions with internal state
              into some special classes which would prevent problems
              (possibly by preventing inlining, etc.)?
              This would make coding much more reliable - which is
              essential in large serious projects.

              P.S. These little things are really important, people.
              They usually make the difference between 99% and 100%
              bug-free software, so I think we shouldn't ignore them.


              --
              Vladimir

              Comment

              • Pete Becker

                #8
                Re: Ambiguous Expression

                Vladimir wrote:[color=blue]
                > Victor Bazarov wrote:
                >[color=green][color=darkred]
                >>>call(++i, ++i);
                >>>
                >>>which can result in both arguments being the same!
                >>>That's not really random to me :)[/color]
                >>
                >>The expression above has undefined behaviour because the object 'i'[/color]
                >
                > has
                >[color=green]
                >>its _stored_value_ changed more than once between sequence points.[/color]
                >
                >
                > Similar situation is with rand() - after inlining, internal value
                > (used to hold random state) is changed more than once - that's why
                > the problem occurs.
                >[/color]

                No, because there's a sequence point before each call to rand.

                --

                Pete Becker
                Dinkumware, Ltd. (http://www.dinkumware.com)

                Comment

                • Victor Bazarov

                  #9
                  Re: Ambiguous Expression

                  Vladimir wrote:[color=blue]
                  > Victor Bazarov wrote:
                  >[color=green][color=darkred]
                  >>>call(++i, ++i);
                  >>>
                  >>>which can result in both arguments being the same!
                  >>>That's not really random to me :)[/color]
                  >>
                  >>The expression above has undefined behaviour because the object 'i'[/color]
                  >
                  > has
                  >[color=green]
                  >>its _stored_value_ changed more than once between sequence points.[/color]
                  >
                  >
                  > Similar situation is with rand() - after inlining, internal value
                  > (used to hold random state) is changed more than once - that's why
                  > the problem occurs.[/color]

                  Similar, but no undefined behaviour, only unspecified order of calls.
                  Every function call is surrounded by sequence points, so even with
                  inlining there would be at least four of them between the program
                  decided to call the first 'rand' and calling the 'call' function. So,
                  the change to some stored value (the side effect of 'rand') does not
                  happen more than once between sequence points.
                  [color=blue][color=green]
                  >>There isn't any. Use separately declared/defined/initialised[/color]
                  >
                  > objects:
                  >[color=green]
                  >> int r1 = rand(), r2 = rand();
                  >> Vect2D v(r1, r2);[/color]
                  >
                  >
                  > I wish we could always remember to use this when needed.[/color]

                  And I wish I were young, slim, and healthy.
                  [color=blue]
                  > What about encapsulating such functions with internal state
                  > into some special classes which would prevent problems
                  > (possibly by preventing inlining, etc.)?[/color]

                  You can try limiting the members of your programming team to using
                  some kind of class for that, or a macro, or whatever would resolve
                  this issue, but the language does not provide a mechanism (yet) to
                  catch all instances of unspecified behaviour. Of course we can always
                  hope for better tools at our disposal...
                  [color=blue]
                  > This would make coding much more reliable - which is
                  > essential in large serious projects.[/color]

                  I believe you could use some kind of "PC-lint"-like code checker that
                  might catch that.
                  [color=blue]
                  > P.S. These little things are really important, people.
                  > They usually make the difference between 99% and 100%
                  > bug-free software, so I think we shouldn't ignore them.[/color]

                  Of course we shouldn't. And _we_ won't. It's the programmers who don't
                  read comp.lang.c++ we should be worrying about :-)

                  V

                  Comment

                  • Vladimir

                    #10
                    Re: Ambiguous Expression


                    Victor Bazarov wrote:[color=blue]
                    > So, the change to some stored value (the side effect of 'rand')
                    > does not happen more than once between sequence points.[/color]

                    Here's smallest code reproducing the problem, where func() represents
                    typical rand() implementation in very simplified form:

                    inline int func()
                    {
                    static int state = 0;
                    return ++state;
                    }

                    int main()
                    {
                    std::cout << func() << func() << std::endl;
                    return 0;
                    }

                    Debug build produces "21" which is ok, but Release build
                    outputs "22" which ruins expected sequence-generating behavior.
                    I tested it with vc++ 6.0 and I'm interested what other compilers
                    would offer (note: global optimizations were heavily used).
                    --
                    Vladimir

                    Comment

                    • Victor Bazarov

                      #11
                      Re: Ambiguous Expression

                      Vladimir wrote:[color=blue]
                      > Victor Bazarov wrote:
                      >[color=green]
                      >>So, the change to some stored value (the side effect of 'rand')
                      >>does not happen more than once between sequence points.[/color]
                      >
                      >
                      > Here's smallest code reproducing the problem, where func() represents
                      > typical rand() implementation in very simplified form:
                      >
                      > inline int func()
                      > {
                      > static int state = 0;
                      > return ++state;
                      > }[/color]

                      To make it compile on all compilers, add:

                      #include <iostream> // for 'std::cout'
                      #include <ostream> // for 'std::endl'
                      [color=blue]
                      >
                      > int main()
                      > {
                      > std::cout << func() << func() << std::endl;
                      > return 0;
                      > }
                      >
                      > Debug build produces "21" which is ok, but Release build
                      > outputs "22" which ruins expected sequence-generating behavior.
                      > I tested it with vc++ 6.0 and I'm interested what other compilers
                      > would offer (note: global optimizations were heavily used).[/color]

                      VC++ v7.1 produces "21" in debug mode and "22" in release mode (no
                      surprises there).

                      VC++ v8.0 Beta produces "21" in both debug and release modes.

                      Which is not to say that any of them are "correct", only that they
                      differ, as they may.

                      Victor

                      Comment

                      • Pete Becker

                        #12
                        Re: Ambiguous Expression

                        Victor Bazarov wrote:[color=blue][color=green]
                        >>[/color][/color]
                        => VC++ v7.1 produces "21" in debug mode and "22" in release mode (no[color=blue]
                        > surprises there).
                        >
                        > VC++ v8.0 Beta produces "21" in both debug and release modes.
                        >
                        > Which is not to say that any of them are "correct", only that they
                        > differ, as they may.
                        >[/color]

                        That is, "21" or "12" is okay, but "22" is definitely wrong, because it
                        violates the rules about sequence points.

                        --

                        Pete Becker
                        Dinkumware, Ltd. (http://www.dinkumware.com)

                        Comment

                        • Victor Bazarov

                          #13
                          Re: Ambiguous Expression

                          Pete Becker wrote:[color=blue]
                          > Victor Bazarov wrote:
                          >[color=green][color=darkred]
                          >>>[/color][/color]
                          > => VC++ v7.1 produces "21" in debug mode and "22" in release mode (no
                          >[color=green]
                          >> surprises there).
                          >>
                          >> VC++ v8.0 Beta produces "21" in both debug and release modes.
                          >>
                          >> Which is not to say that any of them are "correct", only that they
                          >> differ, as they may.
                          >>[/color]
                          >
                          > That is, "21" or "12" is okay, but "22" is definitely wrong, because it
                          > violates the rules about sequence points.[/color]

                          Ah... Good point (no pun intended). So, optimization should not prevent
                          any side effects from taking place, yes? My guess is that circumventing
                          side effects is only allowed in particular cases and they are described in
                          the Standard explicitly.

                          V

                          Comment

                          • Pete Becker

                            #14
                            Re: Ambiguous Expression

                            Victor Bazarov wrote:[color=blue]
                            > Ah... Good point (no pun intended). So, optimization should not prevent
                            > any side effects from taking place, yes? My guess is that circumventing
                            > side effects is only allowed in particular cases and they are described in
                            > the Standard explicitly.[/color]

                            They're not described explicitly, but the "as if" rule (1.5/1) is the
                            thing to look to. The standard specifies the observable behavior of
                            well-formed programs. The behavior of this program depends on
                            unspecified behavior, but that doesn't make it ill-formed, so the
                            compiler must produce a program with the observable behavior specified
                            by the standard. It can't blow away the sequence point after the first
                            call (in the generated code, not the fist in the source code) to func.

                            --

                            Pete Becker
                            Dinkumware, Ltd. (http://www.dinkumware.com)

                            Comment

                            • Vladimir

                              #15
                              Re: Ambiguous Expression

                              So,

                              vc++ v6.0 debug "21", release "22"
                              vc++ v7.1 debug "21", release "22"
                              vc++ v8.0 Beta "21" in both debug and release modes.

                              Something tells me v8.0 *Final* will catch up with previous versions :)
                              ok, it's OT - I'm hiding right now.

                              --
                              Vladimir

                              Comment

                              Working...