HP/UX Itanium C comiler & std C

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

    HP/UX Itanium C comiler & std C

    While porting our application to HP/UX 11.23 Itanium, I came across
    this situation where the compiler acts differently to any other UNIX
    C/C++ compiler that I have come across in the last 10 years (including
    other HP-UX platforms).

    Consider the following code:

    #include <stdio.h>

    int main()
    {
    char *ptr = "AB";
    char *ans;

    ans = (*ptr++) ? ptr : "";

    printf ("Answer is %s\n", ans);

    return 0;
    }

    When running this, I would usually expect to see:

    Answer is B

    On Itanium it displays:

    Answer is AB

    There are a few HPUX compilers to choose from on this platform, but
    they all behave in the same way. Compiler switches don't make any
    difference. I am now using GCC, which works as I expect.

    aCC on HPUX 11 PA-RISC also displays "AB".

    Has anyone come across this before or confirm that the results I've
    seen are invalid for ANSI C?

    Sion.
  • Mark A. Odell

    #2
    Re: HP/UX Itanium C comiler &amp; std C

    sroberts@transo ft.com (Sion Roberts) wrote in
    news:ae5dbf8a.0 411150516.5e626 085@posting.goo gle.com:
    [color=blue]
    > While porting our application to HP/UX 11.23 Itanium, I came across
    > this situation where the compiler acts differently to any other UNIX
    > C/C++ compiler that I have come across in the last 10 years (including
    > other HP-UX platforms).
    >
    > Consider the following code:
    >
    > #include <stdio.h>
    >
    > int main()
    > {
    > char *ptr = "AB";
    > char *ans;
    >
    > ans = (*ptr++) ? ptr : "";
    >
    > printf ("Answer is %s\n", ans);
    >
    > return 0;
    > }
    >
    > When running this, I would usually expect to see:
    >
    > Answer is B[/color]

    No, not unless you used %c instead of %s.[color=blue]
    >
    > On Itanium it displays:
    >
    > Answer is AB[/color]

    This is correct. Strings are null terminated. The char 'B' is not '\0'
    thus it gets printed along with 'A'.

    --
    - Mark ->
    --

    Comment

    • Kenny McCormack

      #3
      Re: HP/UX Itanium C comiler &amp; std C

      In article <Xns95A25B2C33A 1FCopyrightMark Odell@130.133.1 .4>,
      Mark A. Odell <odellmark@hotm ail.com> wrote:[color=blue]
      >sroberts@trans oft.com (Sion Roberts) wrote in
      >news:ae5dbf8a. 0411150516.5e62 6085@posting.go ogle.com:
      >[color=green]
      >> While porting our application to HP/UX 11.23 Itanium, I came across
      >> this situation where the compiler acts differently to any other UNIX
      >> C/C++ compiler that I have come across in the last 10 years (including
      >> other HP-UX platforms).
      >>
      >> Consider the following code:
      >>
      >> #include <stdio.h>
      >>
      >> int main()
      >> {
      >> char *ptr = "AB";
      >> char *ans;
      >>
      >> ans = (*ptr++) ? ptr : "";
      >>
      >> printf ("Answer is %s\n", ans);
      >>
      >> return 0;
      >> }
      >>
      >> When running this, I would usually expect to see:
      >>
      >> Answer is B[/color]
      >
      >No, not unless you used %c instead of %s.[/color]

      Um, %c would print the lowest 8 bits of the pointer value stored in "ans",
      as an ASCII character (on most machines - obviously O/T, of course...)
      [color=blue][color=green]
      >> On Itanium it displays:
      >>
      >> Answer is AB[/color]
      >
      >This is correct. Strings are null terminated. The char 'B' is not '\0'
      >thus it gets printed along with 'A'.[/color]

      The idea of the above is that in:

      ans = (*ptr++) ? ptr : "";

      "ptr" is "supposed" to be incremented after it is tested but before its
      value is fetched and assigned to "ans". The pedants in this group will
      probably point out that this behavior is not required by "the standard".

      Comment

      • Lawrence Kirby

        #4
        Re: HP/UX Itanium C comiler &amp; std C

        On Mon, 15 Nov 2004 05:16:00 -0800, Sion Roberts wrote:
        [color=blue]
        > While porting our application to HP/UX 11.23 Itanium, I came across this
        > situation where the compiler acts differently to any other UNIX C/C++
        > compiler that I have come across in the last 10 years (including other
        > HP-UX platforms).
        >
        > Consider the following code:
        >
        > #include <stdio.h>
        >
        > int main()
        > {
        > char *ptr = "AB";
        > char *ans;
        >
        > ans = (*ptr++) ? ptr : "";
        >
        > printf ("Answer is %s\n", ans);
        >
        > return 0;
        > }
        >
        > When running this, I would usually expect to see:
        >
        > Answer is B[/color]

        Correct, the ?: operator defines a sequence point after its first operand
        is evaluated. Therefore the side-effect of incrementing ptr must be
        complete before the value of ptr is read for the second operand (which it
        is because *ptr++ is 'A' which is not null). So a pointer to the 2nd
        character in the string should be assigned to ans.
        [color=blue]
        > On Itanium it displays:
        >
        > Answer is AB[/color]

        Looks like you've found a bona fide compiler bug.

        Lawrence

        Comment

        • pete

          #5
          Re: HP/UX Itanium C comiler &amp; std C

          Mark A. Odell wrote:[color=blue]
          >
          > sroberts@transo ft.com (Sion Roberts) wrote in
          > news:ae5dbf8a.0 411150516.5e626 085@posting.goo gle.com:
          >[color=green]
          > > While porting our application to HP/UX 11.23 Itanium, I came across
          > > this situation where the compiler acts differently to any other UNIX
          > > C/C++ compiler that I have come across in the last 10 years (including
          > > other HP-UX platforms).
          > >
          > > Consider the following code:
          > >
          > > #include <stdio.h>
          > >
          > > int main()
          > > {
          > > char *ptr = "AB";
          > > char *ans;
          > >
          > > ans = (*ptr++) ? ptr : "";
          > >
          > > printf ("Answer is %s\n", ans);
          > >
          > > return 0;
          > > }
          > >
          > > When running this, I would usually expect to see:
          > >
          > > Answer is B[/color]
          >
          > No, not unless you used %c instead of %s.[/color]

          Answer is B.
          ans points to a string regardless of whether it points
          to the first or second element of "AB".

          --
          pete

          Comment

          • Bjรธrn Augestad

            #6
            Re: HP/UX Itanium C comiler &amp; std C

            Lawrence Kirby wrote:
            [color=blue]
            > On Mon, 15 Nov 2004 05:16:00 -0800, Sion Roberts wrote:
            >
            >[color=green]
            >>While porting our application to HP/UX 11.23 Itanium, I came across this
            >>situation where the compiler acts differently to any other UNIX C/C++
            >>compiler that I have come across in the last 10 years (including other
            >>HP-UX platforms).
            >>
            >>Consider the following code:
            >>
            >> #include <stdio.h>
            >>
            >> int main()
            >> {
            >> char *ptr = "AB";
            >> char *ans;
            >>
            >> ans = (*ptr++) ? ptr : "";
            >>
            >> printf ("Answer is %s\n", ans);
            >>
            >> return 0;
            >> }
            >>
            >>When running this, I would usually expect to see:
            >>
            >> Answer is B[/color]
            >
            >
            > Correct, the ?: operator defines a sequence point after its first operand
            > is evaluated. Therefore the side-effect of incrementing ptr must be
            > complete before the value of ptr is read for the second operand (which it
            > is because *ptr++ is 'A' which is not null). So a pointer to the 2nd
            > character in the string should be assigned to ans.[/color]

            Can someone please clarify what is meant by this text, from C99 ยง6.5.15
            Conditional operator, #4.

            "If an attempt is made to modify the result of a conditional operator or
            to access it after the next sequence point, the behavior is undefined".

            Does the "access it" part apply to the OP's code or am I just
            misinterpreting the standard?

            TIA
            Bjรธrn


            [color=blue]
            >[/color]
            [snip]

            Comment

            • pete

              #7
              Re: HP/UX Itanium C comiler &amp; std C

              Bjรธrn Augestad wrote:[color=blue]
              >
              > Lawrence Kirby wrote:
              >[color=green]
              > > On Mon, 15 Nov 2004 05:16:00 -0800, Sion Roberts wrote:
              > >
              > >[color=darkred]
              > >>While porting our application to HP/UX 11.23 Itanium, I came across this
              > >>situation where the compiler acts differently to any other UNIX C/C++
              > >>compiler that I have come across in the last 10 years (including other
              > >>HP-UX platforms).
              > >>
              > >>Consider the following code:
              > >>
              > >> #include <stdio.h>
              > >>
              > >> int main()
              > >> {
              > >> char *ptr = "AB";
              > >> char *ans;
              > >>
              > >> ans = (*ptr++) ? ptr : "";
              > >>
              > >> printf ("Answer is %s\n", ans);
              > >>
              > >> return 0;
              > >> }
              > >>
              > >>When running this, I would usually expect to see:
              > >>
              > >> Answer is B[/color]
              > >
              > >
              > > Correct, the ?: operator defines a sequence point after its first operand
              > > is evaluated. Therefore the side-effect of incrementing ptr must be
              > > complete before the value of ptr is read for the second operand (which it
              > > is because *ptr++ is 'A' which is not null). So a pointer to the 2nd
              > > character in the string should be assigned to ans.[/color]
              >
              > Can someone please clarify what is meant by this text, from C99 ยง6.5.15
              > Conditional operator, #4.
              >
              > "If an attempt is made to modify the result of a conditional
              > operator[/color]

              I think it means that (a ? b : c) is an rvalue
              and that you can't do something like this: ((a ? b : c)++)
              [color=blue]
              > or to access it after the next sequence point,
              > the behavior is undefined".
              > Does the "access it" part apply to the OP's code or am I just
              > misinterpreting the standard?[/color]

              I don't know what that means.

              --
              pete

              Comment

              • Richard Tobin

                #8
                Re: HP/UX Itanium C comiler &amp; std C

                In article <cnadav$b4g$1@y in.interaccess. com>,
                Kenny McCormack <gazelle@intera ccess.com> wrote:
                [color=blue]
                >The idea of the above is that in:
                >
                > ans = (*ptr++) ? ptr : "";
                >
                >"ptr" is "supposed" to be incremented after it is tested but before its
                >value is fetched and assigned to "ans". The pedants in this group will
                >probably point out that this behavior is not required by "the standard".[/color]

                No. The standard says there is a sequence point after the first operand
                of the ? operator.

                -- Richard

                Comment

                • Lawrence Kirby

                  #9
                  Re: HP/UX Itanium C comiler &amp; std C

                  On Mon, 15 Nov 2004 14:41:45 +0000, pete wrote:
                  [color=blue]
                  > Bjรธrn Augestad wrote:[color=green]
                  >>
                  >> Lawrence Kirby wrote:
                  >>[color=darkred]
                  >> > On Mon, 15 Nov 2004 05:16:00 -0800, Sion Roberts wrote:
                  >> >
                  >> >
                  >> >>While porting our application to HP/UX 11.23 Itanium, I came across this
                  >> >>situation where the compiler acts differently to any other UNIX C/C++
                  >> >>compiler that I have come across in the last 10 years (including other
                  >> >>HP-UX platforms).
                  >> >>
                  >> >>Consider the following code:
                  >> >>
                  >> >> #include <stdio.h>
                  >> >>
                  >> >> int main()
                  >> >> {
                  >> >> char *ptr = "AB";
                  >> >> char *ans;
                  >> >>
                  >> >> ans = (*ptr++) ? ptr : "";
                  >> >>
                  >> >> printf ("Answer is %s\n", ans);
                  >> >>
                  >> >> return 0;
                  >> >> }
                  >> >>
                  >> >>When running this, I would usually expect to see:
                  >> >>
                  >> >> Answer is B
                  >> >
                  >> >
                  >> > Correct, the ?: operator defines a sequence point after its first operand
                  >> > is evaluated. Therefore the side-effect of incrementing ptr must be
                  >> > complete before the value of ptr is read for the second operand (which it
                  >> > is because *ptr++ is 'A' which is not null). So a pointer to the 2nd
                  >> > character in the string should be assigned to ans.[/color]
                  >>
                  >> Can someone please clarify what is meant by this text, from C99 ยง6.5.15
                  >> Conditional operator, #4.
                  >>[/color][/color]

                  (rearranged to bring the standard text together)
                  [color=blue][color=green]
                  >> "If an attempt is made to modify the result of a conditional
                  >> operator or to access it after the next sequence point,
                  >> the behavior is undefined".[/color]
                  >
                  > I think it means that (a ? b : c) is an rvalue
                  > and that you can't do something like this: ((a ? b : c)++)[/color]

                  That is true but I don't think it is what the text is for, because that is
                  caught by a constraint violation for ++ i.e. its operand must be a
                  modifiable lvalue. A constraint violation is the more significant error,
                  if you have a CV then further undefined behaviour is not an issue.

                  I'm wondering if this has something to do with "rvalue" structures e.g.

                  struct foo { int bar; } s1 = { 1 }, s2 = { 2 };

                  (expr ? s1 : s2).bar

                  but I haven't come up with an example that triggers undefined behaviour
                  from 6.5.15p4 and doesn't trip up elsewhere in the standard. That sentence
                  is an addition from C90, so either they spotted a case that hadn't been
                  spotted at the time of C90 or this has something to do with new C99
                  features.
                  [color=blue][color=green]
                  >> Does the "access it" part apply to the OP's code or am I just
                  >> misinterpreting the standard?[/color]
                  >
                  > I don't know what that means.[/color]

                  The standard talks about modifying the RESULT of the conditional
                  operator or accessing after the next sequence point, which the OPs code
                  does not do.

                  Lawrence

                  Comment

                  • Mark A. Odell

                    #10
                    Re: HP/UX Itanium C comiler &amp; std C

                    "Mark A. Odell" <odellmark@hotm ail.com> wrote in
                    news:Xns95A25B2 C33A1FCopyright MarkOdell@130.1 33.1.4:
                    [color=blue][color=green]
                    >> Consider the following code:
                    >>
                    >> #include <stdio.h>
                    >>
                    >> int main()
                    >> {
                    >> char *ptr = "AB";
                    >> char *ans;
                    >>
                    >> ans = (*ptr++) ? ptr : "";
                    >>
                    >> printf ("Answer is %s\n", ans);
                    >>
                    >> return 0;
                    >> }
                    >>
                    >> When running this, I would usually expect to see:
                    >>
                    >> Answer is B[/color]
                    >
                    > No, not unless you used %c instead of %s.[/color]

                    Ignore me. I missed the increment on 'ptr' and the lack of dereference on
                    'ans'.

                    --
                    - Mark ->
                    --

                    Comment

                    • Dennis Handly

                      #11
                      Re: HP/UX Itanium C comiler &amp; std C

                      Richard Tobin (richard@cogsci .ed.ac.uk) wrote:
                      : In article <cnadav$b4g$1@y in.interaccess. com>,
                      : Kenny McCormack <gazelle@intera ccess.com> wrote:
                      : >The idea of the above is that in:
                      : > ans = (*ptr++) ? ptr : "";
                      : >
                      : >"ptr" is "supposed" to be incremented after it is tested but before its
                      : >value is fetched and assigned to "ans". The pedants in this group will
                      : >probably point out that this behavior is not required by "the standard".

                      : No. The standard says there is a sequence point after the first operand
                      : of the ? operator.
                      : -- Richard

                      Right, we finally saw that too. This is CR JAGae79312:
                      Incorrect order of evaluation for: (x++) ? x ...

                      This is fixed in A.05.57:

                      Comment

                      • Christian Bau

                        #12
                        Re: HP/UX Itanium C comiler &amp; std C

                        In article <cnadav$b4g$1@y in.interaccess. com>,
                        gazelle@yin.int eraccess.com (Kenny McCormack) wrote:
                        [color=blue]
                        > The idea of the above is that in:
                        >
                        > ans = (*ptr++) ? ptr : "";
                        >
                        > "ptr" is "supposed" to be incremented after it is tested but before its
                        > value is fetched and assigned to "ans". The pedants in this group will
                        > probably point out that this behavior is not required by "the standard".[/color]

                        Only pedants who don't know C. Those who know C will replace "supposed"
                        with "required" and remove the quotes:

                        "ptr" is absolutely, one hundred percent required to be incremented
                        after it is tested but before its value is fetched and assigned to
                        "ans".

                        Comment

                        • Kenny McCormack

                          #13
                          Re: HP/UX Itanium C comiler &amp; std C

                          In article <christian.ba u-CA4FBA.22093515 112004@slb-newsm1.svr.pol. co.uk>,
                          Christian Bau <christian.bau@ cbau.freeserve. co.uk> wrote:[color=blue]
                          >In article <cnadav$b4g$1@y in.interaccess. com>,
                          > gazelle@yin.int eraccess.com (Kenny McCormack) wrote:
                          >[color=green]
                          >> The idea of the above is that in:
                          >>
                          >> ans = (*ptr++) ? ptr : "";
                          >>
                          >> "ptr" is "supposed" to be incremented after it is tested but before its
                          >> value is fetched and assigned to "ans". The pedants in this group will
                          >> probably point out that this behavior is not required by "the standard".[/color]
                          >
                          >Only pedants who don't know C. Those who know C will replace "supposed"
                          >with "required" and remove the quotes:
                          >
                          >"ptr" is absolutely, one hundred percent required to be incremented
                          >after it is tested but before its value is fetched and assigned to
                          >"ans".[/color]

                          Well, that is good to know. Always nice to hear of instances where
                          sensible behavior is required by the standard.

                          Comment

                          • pete

                            #14
                            Re: HP/UX Itanium C comiler &amp; std C

                            Lawrence Kirby wrote:
                            [color=blue][color=green][color=darkred]
                            > >> >>Consider the following code:
                            > >> >>
                            > >> >> #include <stdio.h>
                            > >> >>
                            > >> >> int main()
                            > >> >> {
                            > >> >> char *ptr = "AB";
                            > >> >> char *ans;
                            > >> >>
                            > >> >> ans = (*ptr++) ? ptr : "";
                            > >> >>
                            > >> >> printf ("Answer is %s\n", ans);
                            > >> >>
                            > >> >> return 0;
                            > >> >> }[/color][/color][/color]
                            [color=blue][color=green][color=darkred]
                            > >> "If an attempt is made to modify the result of a conditional
                            > >> operator or to access it after the next sequence point,
                            > >> the behavior is undefined".[/color][/color][/color]
                            [color=blue][color=green][color=darkred]
                            > >> Does the "access it" part apply to the OP's code or am I just
                            > >> misinterpreting the standard?[/color]
                            > >
                            > > I don't know what that means.[/color]
                            >
                            > The standard talks about modifying the RESULT of the conditional
                            > operator or accessing after the next sequence point,
                            > which the OPs code does not do.[/color]

                            I still don't understand.
                            Would you please change the code in the above program to something
                            which does attempt to access the result of the conditional
                            operator after the next sequence point,
                            to better illustrate what you and the standard are talking about?

                            --
                            pete

                            Comment

                            • pete

                              #15
                              Re: HP/UX Itanium C comiler &amp; std C

                              Christian Bau wrote:
                              [color=blue][color=green]
                              > > ans = (*ptr++) ? ptr : "";[/color][/color]
                              [color=blue]
                              > "ptr" is absolutely, one hundred percent required to be incremented
                              > after it is tested but before its value is fetched and assigned to
                              > "ans".[/color]

                              I don't see ptr being tested.
                              *ptr++ is tested.
                              ptr may be incremented before the test instead of after.
                              It is the result of the postfix increment operation
                              which is the operand of the indirection operator. *(ptr++)

                              --
                              pete

                              Comment

                              Working...