NULL

  • Thread starter Dimitris Kamenopoulos
  • Start date
D

Dimitris Kamenopoulos

Agent said:
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?

NULL is almost certainly defined as
#define NULL 0

So why not? It's just substituted text.
 
A

Agent Mulder

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?

-X
 
J

John Harrison

Dimitris Kamenopoulos said:
NULL is almost certainly defined as
#define NULL 0

So why not? It's just substituted text.

I think this illustrates what a waste of time NULL is. Its pretending to be
a special pointer value, but really it just zero. Don't pretend, use zero.

john
 
R

Ron Natalie

Agent Mulder said:
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.
It's valid in C++, but kind of silly. C++ requires NULL to be a macro
for an integer constant expression evaluating to zero.

Note however, that it's not a good idea in C. C allows (stupidly in my
opinion) the addition of a (void*) cast on the value 0. This won't allow
assignment to numeric types.
 
J

jeffc

John Harrison said:
I think this illustrates what a waste of time NULL is. Its pretending to be
a special pointer value, but really it just zero. Don't pretend, use zero.

Yeah, this is one of those things that seems to obfuscate with no apparent
benefit. I remember seeing some code once where someone who was taught to
define constants for "Everything" got carried away.

#define ONE 1

God, can you imagine? One of the benefits of using a constant is that if a
value ever changes, it can be changed in one place, not many places. What
if the value actually did change?
 
P

Peter van Merkerk

NULL is almost certainly defined as
zero.

Yeah, this is one of those things that seems to obfuscate with no apparent
benefit. I remember seeing some code once where someone who was taught to
define constants for "Everything" got carried away.

#define ONE 1

God, can you imagine?

Yes I can, I know of someone who used macro's to turn a C compiler into a
(sort of) Pascal compiler. Unfortunately he was the only one who could make
sense out of his code. Macros are the ideal tool to obfuscate code, and some
people really do love this kind of thing.
 
W

White Wolf

Peter van Merkerk wrote:
[SNIP]
Yes I can, I know of someone who used macro's to turn a C compiler
into a (sort of) Pascal compiler. Unfortunately he was the only one
who could make sense out of his code. Macros are the ideal tool to
obfuscate code, and some people really do love this kind of thing.

Hehe. A college of mine turned C into Korn shell. His code was something
like this:

if (..) then
fi

It took me (not knowing UNIX at all at that time) half an hour just to learn
what he is doing by reading his obfuscation header.
 
R

Ron Natalie

White Wolf said:
Peter van Merkerk wrote:
[SNIP]
Yes I can, I know of someone who used macro's to turn a C compiler
into a (sort of) Pascal compiler. Unfortunately he was the only one
who could make sense out of his code. Macros are the ideal tool to
obfuscate code, and some people really do love this kind of thing.

Hehe. A college of mine turned C into Korn shell. His code was something
like this:

if (..) then
fi

If you ever saw what the Bourne shell (and certain of Steve's other programs)
looked like. He spent a lot of time making C look like ALGOL or something.
 
N

Noah Roberts

John said:
I think this illustrates what a waste of time NULL is. Its pretending to be
a special pointer value, but really it just zero. Don't pretend, use zero.

IMHO you are wrong. NULL is provided for readability and to express an
idea that is not representable by a number. We don't care what the
number is, what we care is if the pointer is labeled as invalid.

Macro definitions of constants is not just so that you can change the
definition in a single place, but also to convey ideas about the code.
 
A

Agent Mulder

#define ONE 1
Yes I can, I know of someone who used macro's to turn a C compiler into a
(sort of) Pascal compiler. Unfortunately he was the only one who could make
sense out of his code. Macros are the ideal tool to obfuscate code, and some
people really do love this kind of thing.

Yes. It was the THE fun in assembler. Building
your own lego. In C++, #define is among the
more ugly features.

-X
 
R

Rolf Magnus

Ron said:
It's valid in C++, but kind of silly. C++ requires NULL to be a
macro for an integer constant expression evaluating to zero.

Note however, that it's not a good idea in C. C allows (stupidly in
my opinion) the addition of a (void*) cast on the value 0. This
won't allow assignment to numeric types.

Actually, I tend to see this as more logical, since NULL is actually
meant as a macro for null pointers and not for numeric zero values.
 
J

jeffc

Noah Roberts said:
IMHO you are wrong. NULL is provided for readability and to express an
idea that is not representable by a number. We don't care what the
number is, what we care is if the pointer is labeled as invalid.

Macro definitions of constants is not just so that you can change the
definition in a single place, but also to convey ideas about the code.

We hardly need that to tell us what it means. When I listen to soccer,
English announcers say "nil" instead of "zero". Here's what the online
Merriam-Webster dictionary offers:

Null -
6 : of, being, or relating to zero
7 : zero

Zero -
1a: the arithmetical symbol 0 or <null> denoting the absence of all
magnitude or quantity

Sounds pretty much as close to a synonym as you will ever find.
 
R

Ron Natalie

Noah Roberts said:
John Harrison wrote:
IMHO you are wrong. NULL is provided for readability and to express an
idea that is not representable by a number. We don't care what the
number is, what we care is if the pointer is labeled as invalid.

Macro definitions of constants is not just so that you can change the
definition in a single place, but also to convey ideas about the code.
You can't ever change this. The value really is a zero.
 
M

Mike Smith

Agent said:
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?

Yes, since NULL is generally #defined as 0. I suppose now you're going
to tell us that this is proof that C++ is irreparably broken.
 
P

Peter Ammon

John said:
I think this illustrates what a waste of time NULL is. Its pretending to be
a special pointer value, but really it just zero. Don't pretend, use zero.

john

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
 
N

Noah Roberts

jeffc said:
We hardly need that to tell us what it means. When I listen to soccer,
English announcers say "nil" instead of "zero". Here's what the online
Merriam-Webster dictionary offers:

Null -
6 : of, being, or relating to zero
7 : zero

Zero -
1a: the arithmetical symbol 0 or <null> denoting the absence of all
magnitude or quantity

Sounds pretty much as close to a synonym as you will ever find.

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".

NR
 
N

Noah Roberts

Peter 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.

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?

NR
 
K

Kevin Goodsell

Noah 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?

I consider this a decent demonstration of the danger of NULL. The second
line tells you exactly nothing different from the first. They are
completely identical. But it /tricks/ you into assuming 'variable' is a
pointer, when it may not be.

void func(int i);
void func(char *cp);

func(0);

Is func most likely func(int) or func(char *)?

func(NULL);

Is func most likely func(int) or func(char *)?

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".

Any such information you gather may very well be wrong.

-Kevin
 
K

Kevin Goodsell

Noah 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).

No, it's not possible. '\0' must have the value 0.
It is definately more readable if the programmer uses
these representiations to initialize and compare to instead of the
simple value 0.

Perhaps, but NULL is different. For one thing, it's a macro that anybody
can redefine. For another, it give a false impression about its type.
The same goes for true and false...false is 0, so why use it, why not
just use 0 and 1?

Because the types are different, for one thing.

void func(int i);
void func(bool b);

func(0);
func(false);

-Kevin
 
J

Jeremy Collins

jeffc said:
We hardly need that to tell us what it means. When I listen to soccer,
English announcers say "nil" instead of "zero".

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.

Then again, perhaps we could take inspiration from tennis instead and

#DEFINE LOVE 0
 

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,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top