I just read in the time(0) thread that 0 is to be preferred over NULL in C++.
What is the reasoning for this? I'm all for it, but I might need to convince others.
I just read in the time(0) thread that 0 is to be preferred over NULL in C++.
What is the reasoning for this? I'm all for it, but I might need to convince others.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
If I remember correctly, its because NULL isn't automatically defined as zero, even though most of the time it is. If a compiler wanted to define NULL as -13 it can, which is why it's simply safer to just use zero when that is what you want.
NULL can be defined as:
orCode:#define NULL 0
so you can always use 0 when a pointer is required, but not the other way around.Code:#define NULL ((void*)0)
as for the time function:
http://www.cppreference.com/stddate_details.html#time
it takes a pointer, so you can use any of 0 or NULL.Code:time_t time(time_t* time);
EDIT: missed ()
Last edited by glUser3f; 12-04-2003 at 03:27 PM.
0 is used as NULL because almost every OS uses the first memory address for its own stuff, so a program can use it to indicate an invalid address.Originally posted by PJYelton
If I remember correctly, its because NULL isn't automatically defined as zero, even though most of the time it is. If a compiler wanted to define NULL as -13 it can, which is why it's simply safer to just use zero when that is what you want.
I was referring to this particular comment by Prelude:
In C++ the use of the macro NULL is not recommended, so the integer literal 0 is used because in pointer context it is treated as a null pointer.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
NULL is a macro that in C is usuallly either 0 or ((void *)0). in C the cast makes sense, as you can always assign a void* to any pointer type without an explict cast, but not to an int (or float for that matter). In some C implementations (and this may still be valid) NULL would have a numeric value other than zero so NULL is prefered. In C++ 0 the literal is magical and part of the language. If you are on a platform where you need some other binary value for an invalid address it is the resposibility of the language to provide that value. Same deal with an implied cast to a bool. while(p->next) p = p->next; Could break in K&R C on oddball wierdo platforms where NULL is non-zero, in C++ the language makes sure that a pointer to NULL evaluates to false. This may also be true of C99 and later.
type *p=0; // always cool in C++
(int)p == 0; // MAY NOT BE TRUE IN C++
if(p==0 && !p) // always true in C++
Got it, thanks!
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law