Differencebetween If (Null == Pointer) VS If (Pointer == Null)

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

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.

difference between if(pointer) vs if(pointer != NULL) in c++, cpplint issue

No, if pointer is really a pointer type there is no difference, so everything here is a question of coding style. Coding style in turn depends on habits in different communities so there can't be a general recommendation.

I personally prefer the first because it is shorter and more to the point and avoids the use of the bogus macro NULL.

In C NULL can be very different things (integer or pointer) and in C++ its use is even deprecated nowadays. You should at least use nullptr, there.

Can I use if (pointer) instead of if (pointer != NULL)?

You can; the null pointer is implicitly converted into boolean false while non-null pointers are converted into true. From the C++11 standard, section on Boolean Conversions:

A prvalue of arithmetic, unscoped enumeration, pointer, or pointer to member type can be converted to a
prvalue of type
bool. A zero value, null pointer value, or null member pointer value is converted to
false;
any other value is converted to
true
. A prvalue of type
std::nullptr_t
can be converted to a prvalue of
type
bool
; the resulting value is
false
.

What is the difference between if( pointer == NULL) and if(!pointer)?

  1. Are they different for the compiler? If yes, then how?

If pointer indeed has a pointer type, then there is no difference at all for the compiler.


  1. Which of the two is the recommended coding style for general use and why?

There probably are recommendations either way.

A possible advantage of using !pointer is that there is no opportunity to mistype pointer = NULL, which has very different meaning, but is well-formed. Another advantage is that is requires less typing.


  1. Can you please explain how NULL is "seen" by the compiler.

As specified in the standard, NULL is an implementation defined macro, which expands to a null pointer constant.

C++ - Difference between !pointer and pointer == nullptr?

Actually both are same. You can check whether the pointer is null or not using both.

nullptr was introduced in C++11 which can be used like if(p == nullptr) which is preferred over if(!p)

Hope this helps :)

Is NULL data-pointer the same as NULL function-pointer?

C 2018 6.3.2.3 4 says:

Conversion of a null pointer to another pointer type yields a null pointer of that type. Any two null pointers shall compare equal.

This paragraph, unlike paragraph 7, does not limit the conversions to pointers to object types or pointers to function types. Therefore, if a null pointer of some pointer-to-function type is converted to void *, the result is a null pointer, and then applying ! to it yields 1.

Establishing the converse, that if applying ! to a pointer yields 1, it necessarily arose from a null pointer, it more difficult. We could imagine some non-null function pointer that, when converted to void *, yields a null pointer. Considering the intent of POSIX to allow function pointers to be temporarily stored in void *, we can conclude that converting a pointer to a function to void * should never result in a null pointer.

Could the bit pattern for a NULL void* data-pointer ever equate to a non-NULL function-pointer value?

The C standard does not discuss the bit patterns used to represent pointers. The semantics are established in terms of the values.

Would any sane implementation do this?

Certainly bare-metal boot code on some hardware might put executable instructions at address zero and call a function there for some reason and might also use address zero as a null pointer. It will simply be designed not to depend on that function at address zero not being tested for being a null pointer.

Outside of such special situations, i.e., for all practical purposes, this is not done. If some software decides it needs a special representation for a null pointer, it will set aside some address for that and not use that address for any ordinary function or object.

Is there a difference between initialiazing variables and pointers with NULL or 0?

NULL is a pointer constant. You use this to initialize a pointer to a value that says it doesn't point to anything.

On most C implementations, it is defined as:

#define NULL ((void *)0)

But there's no guarantee of that.

Determining null pointer

According to the C++ 14 Standard (5.10 Equality operators)

2 If at least one of the operands is a pointer, pointer conversions
(4.10) and qualification conversions (4.4) are performed on both
operands to bring them to their composite pointer type (Clause 5).
Comparing pointers is defined as follows: Two pointers compare equal
if they are both null, both point to the same function, or both
represent the same address (3.9.2), otherwise they compare unequal.

And (4.10 Pointer conversions)

1 A null pointer constant is an integer literal (2.13.2) with value
zero or a prvalue of type std::nullptr_t. A null pointer constant can
be converted to a pointer type; the result is the null pointer value
of that type and is distinguishable from every other value of object
pointer or function pointer type. Such a conversion is called a null
pointer conversion...

Thus the expression in the return statement

 return parent == 0 ? nullptr : parent->getChild();

is entirely correct because the null pointer constant 0 is converted to a null pointer value of the type of the pointer parent. But it will be more expressive to write

 return parent == nullptr ? nullptr : parent->getChild();


Related Topics



Leave a reply



Submit