• Create Account

### #ActualServant of the Lord

Posted 14 December 2012 - 01:38 PM

[forked from Problems with my custom linked list class]

Don't compare to 0, compare to NULL (or if using C++11, compare to nullptr). It describes your intent better.

Another forummer responded,

...I always thought the same thing you just said there. Though I posted a basic Linked List code couple weeks ago and was using NULL instead of 0 and someone who said he was a TA at a University kept saying that I should not be using NULL in C++, use 0. Good to know that what I learned to use NULL still seems to be right. Also I do use nullptr when I can but the University Compilers don't support C++11 yet.

So I'd like to hear other people's thoughts on it as well.

My thoughts are purely based on describing your code intent. 0 implies working with numbers, whereas NULL implies working with pointers; even though pointers are a number, their usage and purpose is different enough that instantly discerning what is what makes code (very slightly) easier to read.

Example:
this->object = 0;
Is 'object' an integer index into an array where the object is found, or is it a pointer to the object itself? Granted, better naming also helps alleviate this confusion, but there is no denying that strict usage of NULL and 0 would help where improper naming causes confusion.

Looking for an alternative perspective, Bjarne Stroustrup says this:

In C++, the definition of NULL is 0, so there is only an aesthetic difference. I prefer to avoid macros, so I use 0. Another problem with NULL is that people sometimes mistakenly believe that it is different from 0 and/or not an integer. In pre-standard code, NULL was/is sometimes defined to something unsuitable and therefore had/has to be avoided. That's less common these days.
If you have to name the null pointer, call it nullptr; that's what it's called in C++11. Then, "nullptr" will be a keyword.

So his complaints are:
• Macros are evil
• People are ignorant
• Older compilers are broken
All valid-ish points. Here are suggested fixes:

Macros are evil
The problems usually associated with macro mis-use doesn't apply to a single '0'. 0 cannot be accidentally be used in any way that I can think of, which might mess up operator precedence or anything like that.

But if you are absolutely intent on avoiding macros, use a constant:
const size_t MyNull = 0;

People are ignorant
Perhaps, but the key then is to educate them. Either that, or don't let them near your code, since there are more dangerous issues in C++ that ignorance of NULL.

Older compilers are broken
So use a modern compiler? Older compilers mess up in other ways as well. Shall we not use the parts of the standard that older compilers got wrong?

However, he does make a good point about "If you have to name the null pointer, call it nullptr; that's what it's called in C++11."
If, for some reason, you are set against using NULL as a macro, and decided to use a constant, you might as well use this as your constant:
#if __cplusplus < 201103L
const size_t nullptr = 0;
#endif

It'll only be defined (I think) if C++11's nullptr is not available.

When you add C++11's nullptr into the mix, nullptr is obviously the superior choice because not only does it declare your intent, but it also provides additional type-safe protections. But between the outdated NULL and 0, what are your thoughts on the matter?

### #2Servant of the Lord

Posted 14 December 2012 - 01:37 PM

[forked from Problems with my custom linked list class]

Don't compare to 0, compare to NULL (or if using C++11, compare to nullptr). It describes your intent better.

Another forummer responded,

...I always thought the same thing you just said there. Though I posted a basic Linked List code couple weeks ago and was using NULL instead of 0 and someone who said he was a TA at a University kept saying that I should not be using NULL in C++, use 0. Good to know that what I learned to use NULL still seems to be right. Also I do use nullptr when I can but the University Compilers don't support C++11 yet.

So I'd like to hear other people's thoughts on it as well.

My thoughts are purely based on describing your code intent. 0 implies working with numbers, whereas NULL implies working with pointers; even though pointers are a number, their usage and purpose is different enough that instantly discerning what is what makes code (very slightly) easier to read.

Example:
this->object = 0;
Is 'object' an integer index into an array where the object is found, or is it a pointer to the object itself? Granted, better naming also helps alleviate this confusion, but there is no denying that strict usage of NULL and 0 would help where improper naming causes confusion.

Looking for an alternative perspective, Bjarne Stroustrup says this:

In C++, the definition of NULL is 0, so there is only an aesthetic difference. I prefer to avoid macros, so I use 0. Another problem with NULL is that people sometimes mistakenly believe that it is different from 0 and/or not an integer. In pre-standard code, NULL was/is sometimes defined to something unsuitable and therefore had/has to be avoided. That's less common these days.
If you have to name the null pointer, call it nullptr; that's what it's called in C++11. Then, "nullptr" will be a keyword.

So his complaints are:
• Macros are evil
• People are ignorant
• Older compilers are broken
All valid-ish points. Here are suggested fixes:

Macros are evil
The problems usually associated with macro mis-use doesn't apply to a single '0'. 0 cannot be accidentally be used in any way that I can think of, which might mess up operator precedence or anything like that.

But if you are absolutely intent on avoiding macros, use a constant:
const size_t MyNull = 0;

People are ignorant
Perhaps, but the key then is to educate them. Either that, or don't let them near your code, since there are more dangerous issues in C++ that ignorance of NULL.

Older compilers are broken
So use a modern compiler? Older compilers mess up in other ways as well. Shall we not use the parts of the standard that older compilers got wrong?

However, he does make a good point about "If you have to name the null pointer, call it nullptr; that's what it's called in C++11."
If, for some reason, you are set against using NULL as a macro, and decided to use a constant, you might as well use this as your constant:
#if __cplusplus < 201103L
const size_t nullptr = 0;
#endif

It'll only be defined (I think) if C++11's nullptr is not available.

When you add C++11's nullptr into the mix, nullptr is obviously the superior choice because not only does it declare your intent, but it also provides additional type-safe protections. But between the outdated NULL and 0, what are your thoughts on the matter?

### #1Servant of the Lord

Posted 14 December 2012 - 01:29 PM

[forked from Problems with my custom linked list class]

Don't compare to 0, compare to NULL (or if using C++11, compare to nullptr). It describes your intent better.

Another forummer responded,

...I always thought the same thing you just said there. Though I posted a basic Linked List code couple weeks ago and was using NULL instead of 0 and someone who said he was a TA at a University kept saying that I should not be using NULL in C++, use 0. Good to know that what I learned to use NULL still seems to be right. Also I do use nullptr when I can but the University Compilers don't support C++11 yet.

So I'd like to hear other people's thoughts on it as well.

My thoughts are purely based on describing your code intent. 0 implies working with numbers, whereas NULL implies working with pointers; even though pointers are a number, their usage and purpose is different enough that instantly discerning what is what makes code (very slightly) easier to read.

Example:
this->object = 0;
Is 'object' an integer index into an array where the object is found, or is it a pointer to the object itself? Granted, better naming also helps alleviate this confusion, but there is no denying that strict usage of NULL and 0 would help where improper naming causes confusion.

Looking for an alternative perspective, Bjarne Stroustrup says this:

In C++, the definition of NULL is 0, so there is only an aesthetic difference. I prefer to avoid macros, so I use 0. Another problem with NULL is that people sometimes mistakenly believe that it is different from 0 and/or not an integer. In pre-standard code, NULL was/is sometimes defined to something unsuitable and therefore had/has to be avoided. That's less common these days.
If you have to name the null pointer, call it nullptr; that's what it's called in C++11. Then, "nullptr" will be a keyword.

So his complaints are:
• Macros are evil
• People are ignorant
• Older compilers are broken
All valid-ish points. Here are suggested fixes:

Macros are evil
The problems usually associated with macro mis-use doesn't apply to a single '0'. 0 cannot be accidentally be used in any way that I can think of, which might mess up operator precedence or anything like that.

But if you are absolutely intent on avoiding macros, use a constant:
const size_t MyNull = 0;

People are ignorant
Perhaps, but the key then is to educate them. Either that, or don't let them near your code, since there are more dangerous issues in C++ that ignorance of NULL.

Older compilers are broken
So use a modern compiler? Older compilers mess up in other ways as well. Shall we not use the parts of the standard that older compilers got wrong?

However, he does make a good point about "If you have to name the null pointer, call it nullptr; that's what it's called in C++11."
If, for some reason, you are set against using NULL as a macro, and decided to use a constant, you might as well use this as your constant:
#if __cplusplus < 201103L
const size_t nullptr = 0;
#endif

It'll only be defined (I think) if C++11's nullptr is not available.

When you add C++11's nullptr into the mix, nullptr is obviously the superior choice because not only does it declare your intent, but it also provides additional type-safe protections. But between the outdated NULL and 0, what are your thoughts on the matter?

PARTNERS