|
-
October 7th, 2010, 11:03 AM
#13
Re: Coding Standards
 Originally Posted by OReubens
0 is a numeric value of zero. NULL is a pointer value. You should use them appropriately. Using 0 when you mean a pointer value is just bad form.
Additionally writing 0 where you mean NULL may work on compilers now, but it may very well cause problems in the future when suddenly standards decide that char *p = 0; is no longer valid.
It can also be confusing to someone else. Did you mean to make a char pointer that is pointing to memory location 0? Or did you try to set the char pointed to by the pointer p to 0 ?
Actually, 0 is also a null pointer constant, as is NULL, which is a macro that could well be defined as 0. As such, unless the compiler implements some special checks that are not required by the standard, the use of NULL over 0 provides a false sense of security, although it can provide some clarity, if you are willing to trust it.
The real solution is nullptr, which should be part of the next version of C++. Consequently, the argument that there is a possibility that "suddenly standards decide that char *p = 0; is no longer valid" is likely to apply for NULL as well. The argument concerning confusion is weak: if the reader could be confused by that syntax, then changing 0 to NULL does not necessarily remove the confusion, e.g., are you trying to set the char pointed to by the pointer p to NULL?
 Originally Posted by OReubens
The only reason I can see to write 0 instead of NULL is to save typing 3 characters.. But I prefer code clarity over typing a 3 characters a lot more.
Stroustrup's reason is to avoid the use of a macro, where feasible.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|