473,567 Members | 2,896 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

NULL vs. 0

I searched the FAQ on this one, couldn't really find an answer.

Stylistically, is it better to use 0 or NULL?

I know that pre-Standard, 0 was recommended, because some compilers
implemented NULL improperly.

However, 18.1/4(footnote 180) indicates that 0 and 0L are valid NULLs,
while (void*)0 is explicitly invalid.

So, assuming a (reasonably) Standard compliant compiler, is there a
reason to continue to prefer 0 over NULL? NULL would seem to have the
advantage of indicating that we're checking a pointer.

Also, did nullptr make it into the C++0x draft?
Jun 21 '06 #1
37 7016
>From "The C++ Language":

Zero (0) is an int. Because of standard conversions, 0 can be used as a
constant of any integral, floating-point, pointer, or pointer-to-member
type. The type of zero will be determined by context. Zero will
typically (but not always) be represented by a bit-pattern of all-zeros
of the appropriate size.
No object is allocated with the address of 0. Consequently, 0 acts as a
pointer literal, indicating that the pointer doesn't refer to an
object.
In C, it has been popular to define a macro NULL to represent a zero
pointer. Because of C++'s tighter type checking, the use of plain 0,
rather than any suggested NULL macro, leads to fewer problems.
red floyd wrote:
I searched the FAQ on this one, couldn't really find an answer.

Stylistically, is it better to use 0 or NULL?

I know that pre-Standard, 0 was recommended, because some compilers
implemented NULL improperly.

However, 18.1/4(footnote 180) indicates that 0 and 0L are valid NULLs,
while (void*)0 is explicitly invalid.

So, assuming a (reasonably) Standard compliant compiler, is there a
reason to continue to prefer 0 over NULL? NULL would seem to have the
advantage of indicating that we're checking a pointer.

Also, did nullptr make it into the C++0x draft?


Jun 21 '06 #2
ra****@gmail.co m wrote:
red floyd wrote:
I searched the FAQ on this one, couldn't really find an answer.

Stylistically, is it better to use 0 or NULL?

I know that pre-Standard, 0 was recommended, because some compilers
implemented NULL improperly.

However, 18.1/4(footnote 180) indicates that 0 and 0L are valid NULLs,
while (void*)0 is explicitly invalid.

So, assuming a (reasonably) Standard compliant compiler, is there a
reason to continue to prefer 0 over NULL? NULL would seem to have the
advantage of indicating that we're checking a pointer.

Also, did nullptr make it into the C++0x draft?

From "The C++ Language":


Zero (0) is an int. Because of standard conversions, 0 can be used as a
constant of any integral, floating-point, pointer, or pointer-to-member
type. The type of zero will be determined by context. Zero will
typically (but not always) be represented by a bit-pattern of all-zeros
of the appropriate size.
No object is allocated with the address of 0. Consequently, 0 acts as a
pointer literal, indicating that the pointer doesn't refer to an
object.
In C, it has been popular to define a macro NULL to represent a zero
pointer. Because of C++'s tighter type checking, the use of plain 0,
rather than any suggested NULL macro, leads to fewer problems.

1. Please don't top post
(http://www.parashift.com/c++-faq-lit....html#faq-5.4).
Rearranged to conform to c.l.c++ netiquette.

2. I have, and have read TC++PL3e. My point was that because the
Standard 18.1/4 specifies "The macro NULL is an implementation-defined
null pointer constant in this International Standard (4.10)." Footnote
180 to 18.1/4 specifically disallows (void*)0, is there any good modern
reason to use 0 instead of NULL anymore?
Jun 21 '06 #3
red floyd wrote:
...
So, assuming a (reasonably) Standard compliant compiler, is there a
reason to continue to prefer 0 over NULL? NULL would seem to have the
advantage of indicating that we're checking a pointer.
...


For that very reason it makes sense to prefer NULL over 0 in pointer context.
Otherwise, they are equivalent (in such contexts).

--
Best regards,
Andrey Tarasevich
Jun 21 '06 #4
* red floyd:
Footnote
180 to 18.1/4 specifically disallows (void*)0, is there any good modern
reason to use 0 instead of NULL anymore?


NULL generally indicates (to the human reader) a pointer value, in about
the same way that struct indicates (to the human reader) a POD.

When maintaining or adding to some existing code base, I think it would
be prudent to follow the conventions already established in that code.

Otherwise, I can't see much for or against any particular practice,
except that consistency is generally good, and except that I think it
would be ungood to mislead by using NULL for integer 0 or struct for a
very non-POD, non-aggregate class.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jun 21 '06 #5
Andrey Tarasevich wrote:
red floyd wrote:
...
So, assuming a (reasonably) Standard compliant compiler, is there a
reason to continue to prefer 0 over NULL? NULL would seem to have the
advantage of indicating that we're checking a pointer.
...


For that very reason it makes sense to prefer NULL over 0 in pointer context.
Otherwise, they are equivalent (in such contexts).


Thanks Alf and Andrey. As I said, the 0 habit was from pre-Standard
days, based on old versions of TCPPPL (1st and 2nd editions!!!).

Just curious about prevailing style.

Thanks again!
Jun 22 '06 #6

red floyd wrote:

2. I have, and have read TC++PL3e. My point was that because the
Standard 18.1/4 specifies "The macro NULL is an implementation-defined
null pointer constant in this International Standard (4.10)." Footnote
180 to 18.1/4 specifically disallows (void*)0, is there any good modern
reason to use 0 instead of NULL anymore?


You are asking why someone would *still* use 0 instead of NULL, as if
NULL was somehow better (ie maybe more descriptive).

I would rather ask why someone would *still* use NULL instead of 0?
NULL is a macro, 0 is what you are really saying - why hide that with
an unnecessary macro?

By the way, here is a 'gotcha' against using NULL, specifically with
regards to NULL being (incorrectly!) perceived as a null pointer:

void func(int x); // A
void func(char *ptr); // B

void foo()
{
func(NULL); // [1]
func(0); // [2]
}

At [1] which func is called, 'A' or 'B'? What about at [2]?

the 'gotcha' may seem obvious in this example, but when you are
debugging a bunch of code, I prefer to see what is really going on - 0,
not NULL.

Tony

P.S. macros are evil.

Jun 22 '06 #7
gottlobfrege wrote:
I would rather ask why someone would *still* use NULL instead of 0?
NULL is a macro, 0 is what you are really saying - why hide that with
an unnecessary macro?
NULL's macro status is not a ding against it. NULL is a well-established
macro with no reason to collide or parse wrong, as new and non-Standard
macros might.

Use NULL to state your intent as clearly and prouncably as possible.

Using 0 just because "the Standard permits it" is sophomoric pedantry.
By the way, here is a 'gotcha' against using NULL, specifically with
regards to NULL being (incorrectly!) perceived as a null pointer:

void func(int x); // A
void func(char *ptr); // B
The guidelines against gratuitous overloads also apply here...
P.S. macros are evil.


So are 'if' statements, 'goto', 'cout', and anything else that can be
abused.

--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
Jun 22 '06 #8
Phlip wrote:
gottlobfrege wrote:
I would rather ask why someone would *still* use NULL instead of 0?
NULL is a macro, 0 is what you are really saying - why hide that with
an unnecessary macro?
NULL's macro status is not a ding against it. NULL is a well-established
macro with no reason to collide or parse wrong, as new and non-Standard
macros might.

Use NULL to state your intent as clearly and prouncably as possible.


That's self-contradictory. NULL is an integer, but one usually thinks of it
as a pointer, because it is only used in pointer context. So using NULL to
hide the fact that you have an integer is less clear than directly using 0.
Using 0 just because "the Standard permits it" is sophomoric pedantry.


I would say the same about using NULL just because the Standard inherited it
from C.

Jun 22 '06 #9
In article <R4************ ******@newssvr1 4.news.prodigy. com>,
"Phlip" <ph******@yahoo .com> wrote:
NULL's macro status is not a ding against it. NULL is a well-established
macro with no reason to collide or parse wrong, as new and non-Standard
macros might.

Use NULL to state your intent as clearly and prouncably as possible.

Using 0 just because "the Standard permits it" is sophomoric pedantry.


Also, I can easily search for the string NULL in my source code. You don't
realize how important stuff like that is until you've got a million lines
of source code to slog trough trying to find constructs which might tickle
some new compiler bug that was just discovered.
Jun 22 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
45385
by: Agoston Bejo | last post by:
I want to enforce such a constraint on a column that would ensure that the values be all unique, but this wouldn't apply to NULL values. (I.e. there may be more than one NULL value in the column.) How can I achieve this? I suppose I would get the most-hated "table/view is changing, trigger/function may not see it" error if I tried to write a...
3
2111
by: iStrain | last post by:
Hiya. I'm _sure_ this is an FAQ, but Googling hasn't produced the answer in a way I can make sense out of. I know I should get this, but so far no way... I'm creating tables and doing queries in Perl, and Nulls have started to bother me greatly. The first issue is, as far as I understand it, a column should be NOT NULL if it is necessary...
5
4232
by: Mike MacSween | last post by:
This as the row source for a combo: SELECT qryRole.RoleID, qryRole.Role FROM qryRole WHERE (((qryRole.RoleID) Not In (SELECT RoleID FROM qryRoleEvent INNER JOIN qryEvent ON qryRoleEvent.EventID = qryEvent.EventID WHERE ProdID = Forms!frmProductions!ProdID))) ORDER BY qryRole.Role; If there is just one RoleID with a null value in the...
3
7639
by: sathyashrayan | last post by:
The standard confirms that the following initialization of a struct struct node { --- --- } struct node var = {NULL};
102
5937
by: junky_fellow | last post by:
Can 0x0 be a valid virtual address in the address space of an application ? If it is valid, then the location pointed by a NULL pointer is also valid and application should not receive "SIGSEGV" ( i am talking of unix machine ) while trying to read that location. Then how can i distinguish between a NULL pointer and an invalid location ?...
29
3724
by: Jason Curl | last post by:
I've been reading this newsgroup for some time and now I am thoroughly confused over what NULL means. I've read a NULL pointer is zero (or zero typecast as a void pointer), others say it's compiler dependent (and that NULL might be anything, but it is always NULL). The source snippet is below. The question is: - When I use calloc to...
5
24683
by: David Sworder | last post by:
Hi, I've created a UserControl-derived class called MyUserControl that is able to persist and subsequently reload its state. It exposes two methods as follows: public void Serialize(Stream s); public void Deserialize(Stream s); Within the MyUserControl class, there is a field of type MyInnerClass
64
3872
by: yossi.kreinin | last post by:
Hi! There is a system where 0x0 is a valid address, but 0xffffffff isn't. How can null pointers be treated by a compiler (besides the typical "solution" of still using 0x0 for "null")? - AFAIK C allows "null pointers" to be represented differently then "all bits 0". Is this correct? - AFAIK I can't `#define NULL 0x10000' since `void*...
0
5355
by: Aaron Morton | last post by:
I'm working on a IHttpModule that handles the PreSendRequestHeaders event from the HttpApplication, if the event is raised after EndRequest then HttpContext.Current is null. If it is raised before EndRequest (by turning response buffering off) then HttpContext.Current is set. To repo add the following to Global.asax protected void...
46
3627
by: lovecreatesbea... | last post by:
Do you prefer malloc or calloc? p = malloc(size); Which of the following two is right to get same storage same as the above call? p = calloc(1, size); p = calloc(size, 1);
0
7905
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8115
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7960
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6272
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5216
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3648
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2093
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1205
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
930
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.