why "++a" is undefined behaviour ?

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

    why "++a" is undefined behaviour ?

    my friend sent me this programme:

    #include<iostre am>
    int main()
    {
    int a=5;
    std ::cout << ++a
    << "\t"
    << ++a
    <<

    return 0;
    }

    its output should be "6 7" and this i what i get:

    (arnuld@arch ~ )% g++ -ansi -pedantic -Wall -Wextra new2.cpp
    new2.cpp: In function 'int main()':
    new2.cpp:10: warning: operation on 'a' may be undefined
    (arnuld@arch ~ )% ./a.out
    7 7
    (arnuld@arch ~ )%


    to not to relay on "undefined behaviuor" i changed this programme:

    #include<iostre am>

    int
    main()
    {
    int a =
    5;

    int b = +
    +a;
    int c = +
    +a;

    std::cout <<
    b
    <<
    "\t"
    <<
    c
    <<
    std::endl;

    return
    0;
    }


    and now it runs as expected:

    (arnuld@arch ~ )% g++ -ansi -pedantic -Wall -Wextra new.cpp
    (arnuld@arch ~ )% ./a.out
    6 7
    (arnuld@arch ~ )%


    i want to know why the direct use of ++a in std::cout is undefiend
    behaviour" ?

  • gara.matt@gmail.com

    #2
    Re: why &quot;++a&qu ot; is undefined behaviour ?

    On Jul 15, 11:10 pm, arnuld <geek.arn...@gm ail.comwrote:
    my friend sent me this programme:
    >
    #include<iostre am>
    int main()
    {
    int a=5;
    std ::cout << ++a
    << "\t"
    << ++a
    <<
    >
    return 0;
    >
    }
    >
    its output should be "6 7" and this i what i get:
    >
    (arnuld@arch ~ )% g++ -ansi -pedantic -Wall -Wextra new2.cpp
    new2.cpp: In function 'int main()':
    new2.cpp:10: warning: operation on 'a' may be undefined
    (arnuld@arch ~ )% ./a.out
    7 7
    (arnuld@arch ~ )%
    >
    to not to relay on "undefined behaviuor" i changed this programme:
    >
    #include<iostre am>
    >
    int
    main()
    {
    int a =
    5;
    >
    int b = +
    +a;
    int c = +
    +a;
    >
    std::cout <<
    b
    <<
    "\t"
    <<
    c
    <<
    std::endl;
    >
    return
    0;
    >
    }
    >
    and now it runs as expected:
    >
    (arnuld@arch ~ )% g++ -ansi -pedantic -Wall -Wextra new.cpp
    (arnuld@arch ~ )% ./a.out
    6 7
    (arnuld@arch ~ )%
    >
    i want to know why the direct use of ++a in std::cout is undefiend
    behaviour" ?
    It isn't. You can use ++a directly in your cout statement. If you take
    a closer look, your first post didn't have "<< endl;" or a semicolon
    at all which is what caused it.

    Comment

    • szclark@googlemail.com

      #3
      Re: why &quot;++a&qu ot; is undefined behaviour ?

      On Jul 16, 7:10 am, arnuld <geek.arn...@gm ail.comwrote:
      my friend sent me this programme:
      >
      #include<iostre am>
      int main()
      {
      int a=5;
      std ::cout << ++a
      << "\t"
      << ++a
      <<
      >
      return 0;
      >
      }
      >
      its output should be "6 7" and this i what i get:
      >
      (arnuld@arch ~ )% g++ -ansi -pedantic -Wall -Wextra new2.cpp
      new2.cpp: In function 'int main()':
      new2.cpp:10: warning: operation on 'a' may be undefined
      (arnuld@arch ~ )% ./a.out
      7 7
      (arnuld@arch ~ )%
      >
      to not to relay on "undefined behaviuor" i changed this programme:
      >
      #include<iostre am>
      >
      int
      main()
      {
      int a =
      5;
      >
      int b = +
      +a;
      int c = +
      +a;
      >
      std::cout <<
      b
      <<
      "\t"
      <<
      c
      <<
      std::endl;
      >
      return
      0;
      >
      }
      >
      and now it runs as expected:
      >
      (arnuld@arch ~ )% g++ -ansi -pedantic -Wall -Wextra new.cpp
      (arnuld@arch ~ )% ./a.out
      6 7
      (arnuld@arch ~ )%
      >
      i want to know why the direct use of ++a in std::cout is undefiend
      behaviour" ?
      Hi, I think it might have been down to the fact that you did not
      include an endl; statement or even just a ;

      #include<iostre am>
      int main()
      {
      int a = 5;
      std ::cout << ++a << "\t;
      std ::cout << ++a;
      return 0;
      }

      This returned 6 7.

      Hope that helped.

      Comment

      • arnuld

        #4
        Re: why &quot;++a&qu ot; is undefined behaviour ?

        On Jul 16, 11:37 am, szcl...@googlem ail.com wrote:

        Hi, I think it might have been down to the fact that you did not
        include an endl; statement or even just a ;
        >
        #include<iostre am>
        int main()
        {
        int a = 5;
        std ::cout << ++a << "\t;
        std ::cout << ++a;
        return 0;
        >
        }
        >
        This returned 6 7.
        you introduced an extra statement in the programme for producing the
        correct result. why i can not get the same output using one single
        statement ?

        Comment

        • szclark@googlemail.com

          #5
          Re: why &quot;++a&qu ot; is undefined behaviour ?

          On 16 Jul, 07:57, arnuld <geek.arn...@gm ail.comwrote:
          On Jul 16, 11:37 am, szcl...@googlem ail.com wrote:
          Hi, I think it might have been down to the fact that you did not
          include an endl; statement or even just a ;
          >
          #include<iostre am>
          int main()
          {
          int a = 5;
          std ::cout << ++a << "\t;
          std ::cout << ++a;
          return 0;
          >
          }
          >
          This returned 6 7.
          >
          you introduced an extra statement in the programme for producing the
          correct result. why i can not get the same output using one single
          statement ?
          I think this is because the compiler will increment 'a' twice before
          the line is output to the screen, so the output will then be 7 7.

          Comment

          • anon

            #6
            Re: why &quot;++a&qu ot; is undefined behaviour ?

            arnuld wrote:
            my friend sent me this programme:
            >
            #include<iostre am>
            int main()
            {
            int a=5;
            std ::cout << ++a
            << "\t"
            << ++a
            <<
            >
            return 0;
            }
            So, this is only one statement:

            std::cout << ++a << "\t" << ++a << std::endl;

            therefore a is incremented twice before anything is done (ie. something
            is printed to standard output).

            That means you got the correct output.

            I can not back this up with a chapter number from the standard as I do
            not have it, but this is how c++ works with prefix increment operator
            >
            i want to know why the direct use of ++a in std::cout is undefiend
            behaviour" ?
            >
            IMO undefined behavior is something different, not this

            Comment

            • anon

              #7
              Re: why &quot;++a&qu ot; is undefined behaviour ?

              arnuld wrote:
              my friend sent me this programme:
              >
              #include<iostre am>
              int main()
              {
              int a=5;
              std ::cout << ++a
              << "\t"
              << ++a
              <<
              >
              return 0;
              }
              >
              its output should be "6 7" and this i what i get:
              >
              (arnuld@arch ~ )% g++ -ansi -pedantic -Wall -Wextra new2.cpp
              new2.cpp: In function 'int main()':
              new2.cpp:10: warning: operation on 'a' may be undefined
              (arnuld@arch ~ )% ./a.out
              7 7
              (arnuld@arch ~ )%
              >
              Basically the rule in C is that if you have an expression, and somewhere
              in that expression, you use an increment operator on a variable, you
              can't mention that variable elsewhere in the expression.

              If we ignore this, then you got correct values (a is incremented twice,
              and then printed)

              Comment

              • arnuld

                #8
                Re: why &quot;++a&qu ot; is undefined behaviour ?

                On Jul 16, 1:31 pm, anon <a...@no.nowrot e:
                So, this is only one statement:
                std::cout << ++a << "\t" << ++a << std::endl;
                therefore a is incremented twice before anything is done (ie. something
                is printed to standard output).
                That means you got the correct output.
                and i thought this works like this:

                1.) std::cout << ++a << "\t" << ++a << std::endl;

                2.) (std::cout << ++a) << "\t" << ++a << std::endl;

                3.) (std::cout << ++a) returns std::cout as a result and as a side-
                effect it writes the vale of "++a" (== 6) to the the standard output,
                which in this case is my terminal. (from C++ Primer 4/e, section
                chapter 1, 1.2.2)


                4.) (std::cout << "\t") << ++a << std::endl;

                same as step 3

                5.) (std::cout << ++a) << std::endl;

                same as step 3 and this time ++a will be printed (== 7)

                6.) std::cout << std::endl;

                this will produce a newline and will flush the buffer


                where i am wrong ?

                I can not back this up with a chapter number from the standard as I do
                not have it, but this is how c++ works with prefix increment operator
                you can but i trust your information as GCC agrees with you :-)

                IMO undefined behavior is something different, not this
                ouch!

                Comment

                • anon

                  #9
                  Re: why &quot;++a&qu ot; is undefined behaviour ?

                  arnuld wrote:
                  >On Jul 16, 1:31 pm, anon <a...@no.nowrot e:
                  >
                  >So, this is only one statement:
                  >std::cout << ++a << "\t" << ++a << std::endl;
                  >
                  >therefore a is incremented twice before anything is done (ie. something
                  >is printed to standard output).
                  >
                  >That means you got the correct output.
                  >
                  and i thought this works like this:
                  >
                  1.) std::cout << ++a << "\t" << ++a << std::endl;
                  >
                  2.) (std::cout << ++a) << "\t" << ++a << std::endl;
                  >
                  3.) (std::cout << ++a) returns std::cout as a result and as a side-
                  effect it writes the vale of "++a" (== 6) to the the standard output,
                  which in this case is my terminal. (from C++ Primer 4/e, section
                  chapter 1, 1.2.2)
                  >
                  >
                  4.) (std::cout << "\t") << ++a << std::endl;
                  >
                  same as step 3
                  >
                  5.) (std::cout << ++a) << std::endl;
                  >
                  same as step 3 and this time ++a will be printed (== 7)
                  >
                  6.) std::cout << std::endl;
                  >
                  this will produce a newline and will flush the buffer
                  >
                  >
                  where i am wrong ?
                  >
                  Steps 3 and 4 are executed simultaneously, that is is incremented twice,
                  before it is sent to the standard output
                  >
                  >I can not back this up with a chapter number from the standard as I do
                  >not have it, but this is how c++ works with prefix increment operator
                  >
                  you can but i trust your information as GCC agrees with you :-)
                  >
                  >
                  >IMO undefined behavior is something different, not this
                  >
                  ouch!
                  >
                  I tried to cancel this message, but looks like it did not work ;)

                  And you get that warning only with -Wall. But I would rather change the
                  code, then remove that parameter

                  Comment

                  Working...