Will the second be evaluated if the first is NULL?

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

    Will the second be evaluated if the first is NULL?

    Consider the following code:

    struct foo
    {
    char* bar;
    };

    // fooptr is of type foo*
    if(fooptr != NULL && fooptr->bar != NULL)
    {
    cout << fooptr->bar << endl;
    }

    Is the if-statement safe, meaning it won't try to evaluate fooptr->bar if
    fooptr is NULL? Can I make sure it is evaluated from left to right or should
    I have nested if-statements instead?

    Thanks for any replies

    / William Payne


  • Karl Heinz Buchegger

    #2
    Re: Will the second be evaluated if the first is NULL?

    William Payne wrote:[color=blue]
    >
    > Consider the following code:
    >
    > struct foo
    > {
    > char* bar;
    > };
    >
    > // fooptr is of type foo*
    > if(fooptr != NULL && fooptr->bar != NULL)
    > {
    > cout << fooptr->bar << endl;
    > }
    >
    > Is the if-statement safe, meaning it won't try to evaluate fooptr->bar if
    > fooptr is NULL? Can I make sure it is evaluated from left to right or should
    > I have nested if-statements instead?
    >[/color]

    In x && y, it is guaranteed that y is not evaluated if x is already false.
    Same for x || y : if x is already true, y is not evaluated at all.

    Which books are you using, that do not mention 'shortcut evaluation' ?


    --
    Karl Heinz Buchegger
    kbuchegg@gascad .at

    Comment

    • Karl Heinz Buchegger

      #3
      Re: Will the second be evaluated if the first is NULL?

      William Payne wrote:[color=blue]
      >
      > Consider the following code:
      >
      > struct foo
      > {
      > char* bar;
      > };
      >
      > // fooptr is of type foo*
      > if(fooptr != NULL && fooptr->bar != NULL)
      > {
      > cout << fooptr->bar << endl;
      > }
      >
      > Is the if-statement safe, meaning it won't try to evaluate fooptr->bar if
      > fooptr is NULL? Can I make sure it is evaluated from left to right or should
      > I have nested if-statements instead?
      >[/color]

      In x && y, it is guaranteed that y is not evaluated if x is already false.
      Same for x || y : if x is already true, y is not evaluated at all.

      Which books are you using, that do not mention 'shortcut evaluation' ?


      --
      Karl Heinz Buchegger
      kbuchegg@gascad .at

      Comment

      • Julie

        #4
        Re: Will the second be evaluated if the first is NULL?

        William Payne wrote:[color=blue]
        >
        > Consider the following code:
        >
        > struct foo
        > {
        > char* bar;
        > };
        >
        > // fooptr is of type foo*
        > if(fooptr != NULL && fooptr->bar != NULL)
        > {
        > cout << fooptr->bar << endl;
        > }
        >
        > Is the if-statement safe, meaning it won't try to evaluate fooptr->bar if
        > fooptr is NULL? Can I make sure it is evaluated from left to right or should
        > I have nested if-statements instead?[/color]

        You're golden -- yes, the statement is safe.

        The behavior is called 'short circuit', and evaluated left to right.

        Comment

        • Julie

          #5
          Re: Will the second be evaluated if the first is NULL?

          William Payne wrote:[color=blue]
          >
          > Consider the following code:
          >
          > struct foo
          > {
          > char* bar;
          > };
          >
          > // fooptr is of type foo*
          > if(fooptr != NULL && fooptr->bar != NULL)
          > {
          > cout << fooptr->bar << endl;
          > }
          >
          > Is the if-statement safe, meaning it won't try to evaluate fooptr->bar if
          > fooptr is NULL? Can I make sure it is evaluated from left to right or should
          > I have nested if-statements instead?[/color]

          You're golden -- yes, the statement is safe.

          The behavior is called 'short circuit', and evaluated left to right.

          Comment

          • William Payne

            #6
            Re: Will the second be evaluated if the first is NULL?


            "Karl Heinz Buchegger" <kbuchegg@gasca d.at> wrote in message
            news:4076B7DC.4 BEC7B4D@gascad. at...[color=blue]
            > William Payne wrote:[color=green]
            > >
            > > Consider the following code:
            > >
            > > struct foo
            > > {
            > > char* bar;
            > > };
            > >
            > > // fooptr is of type foo*
            > > if(fooptr != NULL && fooptr->bar != NULL)
            > > {
            > > cout << fooptr->bar << endl;
            > > }
            > >
            > > Is the if-statement safe, meaning it won't try to evaluate fooptr->bar[/color][/color]
            if[color=blue][color=green]
            > > fooptr is NULL? Can I make sure it is evaluated from left to right or[/color][/color]
            should[color=blue][color=green]
            > > I have nested if-statements instead?
            > >[/color]
            >
            > In x && y, it is guaranteed that y is not evaluated if x is already false.
            > Same for x || y : if x is already true, y is not evaluated at all.
            >
            > Which books are you using, that do not mention 'shortcut evaluation' ?
            >
            >
            > --
            > Karl Heinz Buchegger
            > kbuchegg@gascad .at[/color]

            Thanks for the quick reply, Karl. No need for a nested if-statement then,
            just as I thought. And to answer your question: I don't have access to a C++
            text except Josuttis Standard Library Book at the moment. =/

            / William Payne


            Comment

            • William Payne

              #7
              Re: Will the second be evaluated if the first is NULL?


              "Karl Heinz Buchegger" <kbuchegg@gasca d.at> wrote in message
              news:4076B7DC.4 BEC7B4D@gascad. at...[color=blue]
              > William Payne wrote:[color=green]
              > >
              > > Consider the following code:
              > >
              > > struct foo
              > > {
              > > char* bar;
              > > };
              > >
              > > // fooptr is of type foo*
              > > if(fooptr != NULL && fooptr->bar != NULL)
              > > {
              > > cout << fooptr->bar << endl;
              > > }
              > >
              > > Is the if-statement safe, meaning it won't try to evaluate fooptr->bar[/color][/color]
              if[color=blue][color=green]
              > > fooptr is NULL? Can I make sure it is evaluated from left to right or[/color][/color]
              should[color=blue][color=green]
              > > I have nested if-statements instead?
              > >[/color]
              >
              > In x && y, it is guaranteed that y is not evaluated if x is already false.
              > Same for x || y : if x is already true, y is not evaluated at all.
              >
              > Which books are you using, that do not mention 'shortcut evaluation' ?
              >
              >
              > --
              > Karl Heinz Buchegger
              > kbuchegg@gascad .at[/color]

              Thanks for the quick reply, Karl. No need for a nested if-statement then,
              just as I thought. And to answer your question: I don't have access to a C++
              text except Josuttis Standard Library Book at the moment. =/

              / William Payne


              Comment

              Working...