Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
108 views
in Technique[技术] by (71.8m points)

c++ - What is the difference between if (NULL == pointer) vs if (pointer == NULL)?

What is the difference between using:

if (NULL == pointer) 

and using:

if (pointer == NULL)    

My professor says to use the former over the latter but I don't see the difference between the two.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

There is no difference. What your professor prefers is called Yoda conditions also see "Yoda Conditions", "Pokémon Exception Handling" and other programming classics.

It is supposed to prevent the usage of assignment(=) by mistake over equality(==) in a comparison, but modern compilers should warn about this now, so this type of defensive programming should not be needed. For example:

if( pointer = NULL )

will assign NULL to pointer when what the programmer really meant was:

if( pointer == NULL )

it should have been a comparison, Oops. Make this an error using Yoda conditions you will (see it live), with a similar message to this:

error: expression is not assignable

As jrok points out using:

if (!pointer)

avoids this problem all together in this case.

Here is a concrete example of why with a modern compilers we don't need this technique anymore(see it live):

#include <iostream>

int main()
{
    int *ptr1 = NULL ;

    if( ptr1 = NULL )
    {
            std::cout << "It is NULL" << std::endl ;
    }

}

Note all the warnings:

warning: using the result of an assignment as a condition without parentheses [-Wparentheses]
    if( ptr1 = NULL )
        ~~~~~^~~~~~

note: place parentheses around the assignment to silence this warning
    if( ptr1 = NULL )
             ^
        (          )

use '==' to turn this assignment into an equality comparison
    if( ptr1 = NULL )
             ^
             ==

which makes it pretty hard to miss the problem. It is worth noting that in C++ nullptr should be preferred over NULL, you can look at What are the advantages of using nullptr? for all the details.

Note, in C++ there is the unlikely case with operator overloading that there could be some contrived case where they are not the same.

Note, the -Wparentheses warning in some ways forces a style choice, you either need to give up potentially valid uses of assignment in places where the warning is generated, for example if you use -Werror or choose to parenthesize those cases, which some may find ugly as the comment below suggests. We can turn of the warning in gcc and clang using -Wno-parentheses but I would not recommend that choice since the warning in general will indicate a real bug.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...