evaulation of boolean conditions...

Discussion in 'C++' started by John Ratliff, Jul 21, 2005.

  1. John Ratliff

    John Ratliff Guest

    Does C++ employ lazy evaluation of boolean conditions, e.g. can I do the
    following safely

    if ((obj != NULL) || (obj->someMethod())) {
    // blah blah
    }

    Or will I get a segfault if the obj ptr is NULL? This is fine in Java, but I
    wasn't sure about C++.

    Thanks,

    --John Ratliff
     
    John Ratliff, Jul 21, 2005
    #1
    1. Advertisements

  2. John Ratliff

    John Ratliff Guest

    I meant &&, not || in my condition.

    if ((obj != NULL) && (obj->someMethod())) {
    // blah blah
    }
     
    John Ratliff, Jul 21, 2005
    #2
    1. Advertisements

  3. Yes, C++ uses short circuit OR, i.e If the first expression turns out
    to be false , it wont evaluate further expressions.
    - Nirmalya
     
    Nirmalya Ghosh Chowdhury, Jul 21, 2005
    #3
  4. John Ratliff

    John Ratliff Guest

    Thanks.

    --John Ratliff
     
    John Ratliff, Jul 21, 2005
    #4
  5. John Ratliff

    upashu2 Guest

    In C++The first operand ( a) is completely evaluated and all side effects are
    completed before continuing evaluation of the logical AND expression.

    The second operand (b) is evaluated only if the first operand
    evaluates to true (nonzero). This evaluation eliminates needless
    evaluation of the second operand when the logical AND expression is
    false. You can use this short-circuit evaluation to prevent
    null-pointer dereferencing, as shown in the following example:
    ..
    if ((obj != NULL) && (obj->someMethod()))
    If obj is null (0), the right side of the expression is never
    evaluated.
     
    upashu2, Jul 21, 2005
    #5
  6. John Ratliff

    Srini Guest

    Does C++ employ lazy evaluation of boolean conditions, e.g. can I do the
    Yes. If obj is NULL, the second part of the conditional will not be
    evaluated.

    Regards,
    Srini
     
    Srini, Jul 21, 2005
    #6
  7. John Ratliff

    ambar.shome Guest

    I think this is OK.
    Provided you have instantiated your class in this way using new
    operator

    Obj *obj = new Obj();
     
    ambar.shome, Jul 21, 2005
    #7
  8. John Ratliff

    Greg Guest

    But there's a catch.

    The correct answer is that righthand side of the && expression in the
    sample code provided ( (obj->someMethod()))) may or may not be
    evaluated even when objc != NULL is false. There is not enough in the
    sample provided to answer for certain.

    Only the built-in && operator is guaranteed not to evaluate the
    righthand side of the expression if the lefthand side is false. A
    user-defined && operator would always force an evaluation of the
    righthand side of its expression since it is passed as a parameter to
    the operator&& routine.

    Since the code above omits obj's declaration, the && operator (and the
    != operator as well) could have been overloaed in the case that obj is
    declared as some kind of a smart pointer type.

    Greg
     
    Greg, Jul 21, 2005
    #8
  9. John Ratliff

    Geo Guest

    I think you meant AND, OR will will stop at the first 'true' expression.
     
    Geo, Jul 21, 2005
    #9
  10. John Ratliff

    msalters Guest

    schreef:
    No, it doesn't matter how it was created. The following is also OK

    void foo( Obj* obj )
    {
    if( Obj && Obj->SomeBool )
    // ....
    }
    void bar( )
    {
    Obj o;
    foo( &o );
    }

    BTW, you know that (new Obj) never returns 0? If it fails it throws
    std::bad_alloc so you don't have to test it.

    HTH
    Michiel Salters
     
    msalters, Jul 21, 2005
    #10
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.