Is "?" a sequence point?

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

    Is "?" a sequence point?

    (From something brought up on "Help with array/pointer segmentation
    fault needed" thread.)

    Is "?" a sequence point?

    Or, more directly, is the following defined?

    /* Will "ptr" be guaranteed to have been assigned before the "?"
    * part is evaluated?
    */
    foo = ( ( ptr = some_long_expre ssion ) != NULL )
    ? ptr->bar
    : NULL;

    What about this?

    /* Is the increment of x guaranteed to be complete before the
    * "?" part is evaluated?
    */
    foo = ( ++x == end ) ? x : y;

    --
    +-------------------------+--------------------+-----------------------+
    | Kenneth J. Brody | www.hvcomputer.com | #include |
    | kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer .h> |
    +-------------------------+--------------------+-----------------------+
    Don't e-mail me at: <mailto:ThisIsA SpamTrap@gmail. com>

  • deepak

    #2
    Re: Is &quot;?&quot ; a sequence point?

    Hi Kennedy,

    The increment is guarenteed, because after doing the comparison only we
    can go for the
    next part. I mean the code after '?'.

    In the first case also the same 'll happen.

    Deepak.

    Kenneth Brody wrote:[color=blue]
    > (From something brought up on "Help with array/pointer segmentation
    > fault needed" thread.)
    >
    > Is "?" a sequence point?
    >
    > Or, more directly, is the following defined?
    >
    > /* Will "ptr" be guaranteed to have been assigned before the "?"
    > * part is evaluated?
    > */
    > foo = ( ( ptr = some_long_expre ssion ) != NULL )
    > ? ptr->bar
    > : NULL;
    >
    > What about this?
    >
    > /* Is the increment of x guaranteed to be complete before the
    > * "?" part is evaluated?
    > */
    > foo = ( ++x == end ) ? x : y;
    >
    > --
    > +-------------------------+--------------------+-----------------------+
    > | Kenneth J. Brody | www.hvcomputer.com | #include |
    > | kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer .h> |
    > +-------------------------+--------------------+-----------------------+
    > Don't e-mail me at: <mailto:ThisIsA SpamTrap@gmail. com>[/color]

    Comment

    • Harald van Dijk

      #3
      Re: Is &quot;?&quot ; a sequence point?

      > Kenneth Brody wrote:[color=blue][color=green]
      > > (From something brought up on "Help with array/pointer segmentation
      > > fault needed" thread.)
      > >
      > > Is "?" a sequence point?[/color][/color]

      Yes. "The first operand is evaluated; there is a sequence point after
      its evaluation."
      [color=blue][color=green]
      > > Or, more directly, is the following defined?
      > >
      > > /* Will "ptr" be guaranteed to have been assigned before the "?"
      > > * part is evaluated?
      > > */
      > > foo = ( ( ptr = some_long_expre ssion ) != NULL )
      > > ? ptr->bar
      > > : NULL;[/color][/color]

      Yes, that is guaranteed.
      [color=blue][color=green]
      > > What about this?
      > >
      > > /* Is the increment of x guaranteed to be complete before the
      > > * "?" part is evaluated?
      > > */
      > > foo = ( ++x == end ) ? x : y;[/color][/color]

      Yes again.

      deepak wrote:[color=blue]
      > Hi Kennedy,
      >
      > The increment is guarenteed, because after doing the comparison only we
      > can go for the
      > next part.[/color]

      If there were no sequence point, it would be entirely possible to do
      the store to x after doing the comparison. ++x is equivalent to x += 1,
      so the below applies here.
      [color=blue]
      > I mean the code after '?'.
      >
      > In the first case also the same 'll happen.[/color]

      If there were no sequence point, it would be entirely legitimate to do
      the store to ptr after doing the comparison. Ignoring possible special
      behaviour with volatile objects, the result of (a = b) is b, converted
      to the type of a. It is possible to get this result without even
      evaluating a, let alone actually storing a value in it. The only
      guarantee you have is that the store will be done before the next
      sequence point, and there are real-world cases where behaviour by
      compilers will give different results than your logic would.

      Comment

      • Ben Pfaff

        #4
        Re: Is &quot;?&quot ; a sequence point?

        Kenneth Brody <kenbrody@spamc op.net> writes:
        [color=blue]
        > Is "?" a sequence point?[/color]

        6.5.15 Conditional operator
        Syntax
        1 conditional-expression:
        logical-OR-expression
        logical-OR-expression ? expression : conditional-expression

        [...]

        Semantics
        4 The first operand is evaluated; there is a sequence point after its evaluation.

        --
        "To get the best out of this book, I strongly recommend that you read it."
        --Richard Heathfield

        Comment

        • Skarmander

          #5
          Re: Is &quot;?&quot ; a sequence point?

          Kenneth Brody wrote:[color=blue]
          > (From something brought up on "Help with array/pointer segmentation
          > fault needed" thread.)
          >
          > Is "?" a sequence point?
          >
          > Or, more directly, is the following defined?
          >
          > /* Will "ptr" be guaranteed to have been assigned before the "?"
          > * part is evaluated?
          > */
          > foo = ( ( ptr = some_long_expre ssion ) != NULL )
          > ? ptr->bar
          > : NULL;
          >
          > What about this?
          >
          > /* Is the increment of x guaranteed to be complete before the
          > * "?" part is evaluated?
          > */
          > foo = ( ++x == end ) ? x : y;
          >[/color]

          Notwithstanding the topical answers given, and the obvious need to know, the
          actual answer to this question ought to be "you shouldn't have to be
          required to care, so don't do that".

          Is there a separate word yet for the incurable urge to cram as much
          side-effects into one expression as possible? Somehow "terseness" isn't
          descriptive enough.

          S.

          Comment

          • Eric Sosman

            #6
            Re: Is &quot;?&quot ; a sequence point?



            Skarmander wrote On 06/09/06 14:08,:[color=blue]
            > Kenneth Brody wrote:
            >[color=green]
            >>(From something brought up on "Help with array/pointer segmentation
            >>fault needed" thread.)
            >>
            >>Is "?" a sequence point?
            >>
            >>Or, more directly, is the following defined?
            >>
            >> /* Will "ptr" be guaranteed to have been assigned before the "?"
            >> * part is evaluated?
            >> */
            >> foo = ( ( ptr = some_long_expre ssion ) != NULL )
            >> ? ptr->bar
            >> : NULL;
            >>
            >>What about this?
            >>
            >> /* Is the increment of x guaranteed to be complete before the
            >> * "?" part is evaluated?
            >> */
            >> foo = ( ++x == end ) ? x : y;
            >>[/color]
            >
            >
            > Notwithstanding the topical answers given, and the obvious need to know, the
            > actual answer to this question ought to be "you shouldn't have to be
            > required to care, so don't do that".
            >
            > Is there a separate word yet for the incurable urge to cram as much
            > side-effects into one expression as possible? Somehow "terseness" isn't
            > descriptive enough.[/color]

            Personally, I see nothing wrong with

            value = (ptr == NULL) ? 0 : ptr->value;

            .... and find it easier to read than some of the lengthier
            alternatives. Terseness should not be a goal in itself
            (although 'tis said "brevity is the soul of wit"), but
            neither should verbosity, prolixity, unnecessary redundant
            persiflage, wordiness, and verbosity (if you're only half-
            brief, you're a half-wit).

            A fellow who worked for me once was a native speaker
            of one of the agglutinative languages, and had the habit
            of jamming many words together to form enormous identifiers.
            (How enormous? When we ported the program to a system where
            external identifiers were only significant in their first
            thirty-two characters, his stuff ran afoul of the limit.)

            He was a smart and subtle coder, but his stuff was very
            nearly unreadable simply because of the number of characters
            you had to digest. His `for' usually required three lines
            all to itself and never fewer than two, and no assignment
            statement with more than two operators on the r.h.s. could
            fit on a single line. Try to read

            secondaryFragme ntImpactTime = (
            sqrt(secondaryF ragmentInitialV elocityX
            * secondaryFragme ntInitialVeloci tyX
            - 4 * secondaryFragme ntAccelerationX
            * secondaryFragme ntImpactOffsetX )
            - secondaryFragme ntInitialVeloci tyX)
            / (2 * secondaryFragme ntAccelerationX );

            .... and ponder whether you'd have preferred to read

            t = (sqrt(b*b - 4*a*c) - b) / (2 * a);

            Terseness shouldn't be an end, but it's a good means.

            --
            Eric.Sosman@sun .com

            Comment

            • Skarmander

              #7
              Re: Is &quot;?&quot ; a sequence point?

              Eric Sosman wrote:[color=blue]
              >
              > Skarmander wrote On 06/09/06 14:08,:[color=green]
              >> Kenneth Brody wrote:
              >>[color=darkred]
              >>> (From something brought up on "Help with array/pointer segmentation
              >>> fault needed" thread.)
              >>>
              >>> Is "?" a sequence point?
              >>>
              >>> Or, more directly, is the following defined?
              >>>
              >>> /* Will "ptr" be guaranteed to have been assigned before the "?"
              >>> * part is evaluated?
              >>> */
              >>> foo = ( ( ptr = some_long_expre ssion ) != NULL )
              >>> ? ptr->bar
              >>> : NULL;
              >>>
              >>> What about this?
              >>>
              >>> /* Is the increment of x guaranteed to be complete before the
              >>> * "?" part is evaluated?
              >>> */
              >>> foo = ( ++x == end ) ? x : y;
              >>>[/color]
              >>
              >> Notwithstanding the topical answers given, and the obvious need to know, the
              >> actual answer to this question ought to be "you shouldn't have to be
              >> required to care, so don't do that".
              >>
              >> Is there a separate word yet for the incurable urge to cram as much
              >> side-effects into one expression as possible? Somehow "terseness" isn't
              >> descriptive enough.[/color]
              >
              > Personally, I see nothing wrong with
              >
              > value = (ptr == NULL) ? 0 : ptr->value;
              >[/color]
              Number of side-effects, excluding the top level statement: 0.

              I'm specifically talking about the examples above, or really anything that
              requires you to know what a sequence point is.
              [color=blue]
              > ... and find it easier to read than some of the lengthier
              > alternatives. Terseness should not be a goal in itself
              > (although 'tis said "brevity is the soul of wit"), but
              > neither should verbosity, prolixity, unnecessary redundant
              > persiflage, wordiness, and verbosity (if you're only half-
              > brief, you're a half-wit).
              >[/color]
              While this applies without reservation to natural languages, matters are
              slightly different for programming languages. Clarity of expression and not
              repeating yourself are universally important, but other aspects do not
              transfer as clearly. I've yet to hear much linguistic debate on sequence
              points.

              <snip>[color=blue]
              > He was a smart and subtle coder, but his stuff was very
              > nearly unreadable simply because of the number of characters
              > you had to digest. His `for' usually required three lines
              > all to itself and never fewer than two, and no assignment
              > statement with more than two operators on the r.h.s. could
              > fit on a single line. Try to read
              >
              > secondaryFragme ntImpactTime = (
              > sqrt(secondaryF ragmentInitialV elocityX
              > * secondaryFragme ntInitialVeloci tyX
              > - 4 * secondaryFragme ntAccelerationX
              > * secondaryFragme ntImpactOffsetX )
              > - secondaryFragme ntInitialVeloci tyX)
              > / (2 * secondaryFragme ntAccelerationX );
              >
              > ... and ponder whether you'd have preferred to read
              >
              > t = (sqrt(b*b - 4*a*c) - b) / (2 * a);
              >
              > Terseness shouldn't be an end, but it's a good means.
              >[/color]

              All this I do not dispute, although it's largely orthogonal to the
              particular windmill I was tilting at.

              S.

              Comment

              • Kenneth Brody

                #8
                Re: Is &quot;?&quot ; a sequence point?

                Kenneth Brody wrote:[color=blue]
                >
                > (From something brought up on "Help with array/pointer segmentation
                > fault needed" thread.)
                >
                > Is "?" a sequence point?[/color]
                [...]

                Thanks to all those who responded, including C&V.

                --
                +-------------------------+--------------------+-----------------------+
                | Kenneth J. Brody | www.hvcomputer.com | #include |
                | kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer .h> |
                +-------------------------+--------------------+-----------------------+
                Don't e-mail me at: <mailto:ThisIsA SpamTrap@gmail. com>


                Comment

                Working...