NULL

  • Thread starter Dimitris Kamenopoulos
  • Start date
J

jeffc

Noah Roberts said:
variable = 0;

Is variable most likely a pointer or a simple variable?

variable = NULL;

Is variable most likely a pointer or a simple variable?

Now, of course you can do both with either but when you see "var = NULL"
then as a code reader you can gather much more about 'var' than if it is
"var = 0".

Bad way to go about gathering info.
 
J

jeffc

Peter Ammon said:
The expression "someValue = 0" conveys to me that someValue is an
arithmetic type, whereas "someValue = NULL" conveys to me that someValue
is a pointer type.

That's only because that's what you're used to.
I find this distinction useful for reading code, and
I'll bet a lot of other programmers do to.

I don't think they should.
 
J

jeffc

Noah Roberts said:
Yes, those are all very valuable distinctions yet all are representative
of the same value (though in the case of '\0' it is possible that it is
not actually 0). It is definately more readable if the programmer uses
these representiations to initialize and compare to instead of the
simple value 0.

The same goes for true and false...false is 0, so why use it, why not
just use 0 and 1?

In C++, there is a bool type. I think this is a good thing. There are
predefined values for bool - true and false. This is also a good thing.
I'm glad they did away with the 0 and 1 (or whatever other confusing values
were used for "booleans"). However, they have no such scheme for pointers.
I think it would be great if there were special, built in values to assign
to pointers. But until there are, we ought to use the actual numeric
values. NULL is not part of the language. If and when that happens, I'll
be all for it. But it wouldn't be that simple.
 
J

Jakob Bieling

Noah Roberts said:
variable = 0;

Is variable most likely a pointer or a simple variable?

variable = NULL;

Is variable most likely a pointer or a simple variable?

Now, of course you can do both with either but when you see "var = NULL"
then as a code reader you can gather much more about 'var' than if it is
"var = 0".


A reasonably decent IDE will tell you the type just by pointing at that
variable name either by mouse or by cursor.

regards
 
J

Jakob Bieling

NULL is not part of the language. If and when that happens, I'll
be all for it. But it wouldn't be that simple.


I disagree in that it is not part of the language. Though I agree that
there is room for improvement. Actually, imo, it would suffice to make NULL
a special 'thing', just like 'true' and 'false' are.
Get rid of the #define and allow 'NULL' to be assigned to pointer values
only. While 0 should also be allowed in order to not break older code.
Sounds like a simple change to me.

regars
 
J

jeffc

Jakob Bieling said:
I disagree in that it is not part of the language. Though I agree that
there is room for improvement.

I don't understand. You are saying that NULL is part of the language?
Actually, imo, it would suffice to make NULL
a special 'thing', just like 'true' and 'false' are.

That would be fine in theory, but in practice it's not.
 
I

Ivan Vecerina

| The expression "someValue = 0" conveys to me that someValue is an
| arithmetic type, whereas "someValue = NULL" conveys to me that someValue
| is a pointer type. I find this distinction useful for reading code, and
| I'll bet a lot of other programmers do to.
|
| Similarly, I prefer "someValue = 0." if someValue is a floating point
| type and "someValue = 0" if someValue is an integral type other than
| char, and "someValue = '\0'" if someValue is a char.

But 0. is a floating point type, and '\0' is a char type.
NULL is just 0 (or eventually (0-0), or whatever), which is misleading.

Additionally, NULL is a macro, and shouldn't be used unless an adequate
standard header has been included.


This said, I agree that for notational convenience, it can be nice
to distinguish a 0-pointer from a 0-integer.
To this end, I have settled with using 00 as a notation for null-pointer
values. That was a couple years ago, and I am pretty happy with it.

s/NULL/00/ ;)

Regards,
Ivan
 
R

Ron Natalie

Ivan Vecerina said:
But 0. is a floating point type, and '\0' is a char type.
NULL is just 0 (or eventually (0-0), or whatever), which is misleading.

You can't use 0. for null, but you can use '\0'. A char literal meets the
requirements of an integer constant expression.
This said, I agree that for notational convenience, it can be nice
to distinguish a 0-pointer from a 0-integer.
To this end, I have settled with using 00 as a notation for null-pointer
values. That was a couple years ago, and I am pretty happy with it.
Don't you confuse that with octal zero? :)
 
K

Kevin Goodsell

jeffc said:
I don't understand. You are saying that NULL is part of the language?

It is part of the language. It's presence is mandated by the language
standard. But I understand what you mean.

-Kevin
 
K

Kevin Goodsell

Ron said:
You can't use 0. for null, but you can use '\0'. A char literal meets the
requirements of an integer constant expression.

I don't think Ivan was saying that. I think his point was just that 0.
and '\0' have the "expected" types, while NULL is not actually a
pointer, but an integer.

-Kevin
 
J

Jack Walker

Peter Ammon said:
The expression "someValue = 0" conveys to me that someValue is an
arithmetic type, whereas "someValue = NULL" conveys to me that someValue
is a pointer type. I find this distinction useful for reading code, and
I'll bet a lot of other programmers do to.

Similarly, I prefer "someValue = 0." if someValue is a floating point
type and "someValue = 0" if someValue is an integral type other than
char, and "someValue = '\0'" if someValue is a char.

-Peter

Yeah well, '\0' is not a macro while NULL is. '\0' can not be
redefined to any arbitrary value. Similarly for 0.0F, 0.0d, 0L ...

Jack Walker
 
A

Andre Kostur

A reasonably decent IDE will tell you the type just by pointing at
that
variable name either by mouse or by cursor.

Doesn't help on a printout. I'm personally prefer using NULL to 0. I like
the extra reminder that I'm dealing with a pointer.
 
N

Noah Roberts

Jeremy said:
I use Borland Delphi (IOW Pascal) quite a lot, and this defines the
reserved word "nil", which I prefer over "NULL", as it is reserved for
pointers unless you cast it, and is highlighted in the editor like
other reserved words.

nil is also used in Objective-C, but it is a little different than NULL.

NR
 
J

jeffc

Andre Kostur said:
Doesn't help on a printout. I'm personally prefer using NULL to 0. I like
the extra reminder that I'm dealing with a pointer.

Really?
p = NULL;

There, there's a "reminder" that you're using a pointer. Now, are you
really?
 
N

Noah Roberts

jeffc said:
Really?
p = NULL;

There, there's a "reminder" that you're using a pointer. Now, are you
really?

It is true that p could be anything, not necissarily a pointer. It is
also true that there are idiots out there that might use NULL to
initialize something not a pointer, or compare NULL to an integer.
However, nobody said this was foolproof; what is? What was said is that
it aids in readibility. There are plenty of practices that are normally
good that turn bad when in the wrong hands...that is just life.

NR
 
N

Noah Roberts

Noah said:
It is true that p could be anything, not necissarily a pointer. It is
also true that there are idiots out there that might use NULL to
initialize something not a pointer, or compare NULL to an integer.
However, nobody said this was foolproof; what is? What was said is that
it aids in readibility. There are plenty of practices that are normally
good that turn bad when in the wrong hands...that is just life.

NR

Here is an interesting definition I found on the internet:

http://gcc.gnu.org/ml/gcc/1999-03/msg00155.html

/* I prefer this NIL macro to present a NIL pointer than most other
variations that I have seen (eg, NULL, 0, or (cast) 0.
Comme ci, comme ca. - larry gensch
*/
#ifndef NIL
# define NIL(type) (type *) 0
#endif
 
B

Bob Hairgrove

Hi group,
When filling in my Windows(tm) structs, I use
NULL for object pointers and 0 for ints, floats etc.
I found out that this compiles:

int a=NULL;
float b=NULL;
etc.

Is that valid?

As others have pointed out, NULL is usually just:
#define NULL 0
so there's nothing "wrong" with it.

However, whether it's a good idea or not is a different question. It's
my personal preference only to use NULL if I am initializing (or
assigning to) a pointer. After all, 0 might be a significant value for
an int ... NULL is a signal to me when reading the source code that
the variable holding it is somehow not valid, empty or otherwise in
need of initialization.
 
I

Ivan Vecerina

Ron Natalie said:
You can't use 0. for null, but you can use '\0'. A char literal meets the
requirements of an integer constant expression.
Agreed. But my point was just about NULL not having a type
specifically related to pointers.
Don't you confuse that with octal zero? :)

Well, as I understand the C++ grammar in the C++98 std, 0 itself
might well be an octal literal ;)

But the real point is, while I have often seen 0x00 used to
specifically represent a byte's value, I have yet to see 00
used to represent an integer value.
So using it to represent a null pointer value seems like
a slight improvement over plain 0, in terms of expressiveness
and readability.

Kind regards,
Ivan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top