Connecting Tech Pros Worldwide Forums | Help | Site Map

NULL

Agent Mulder
Guest
 
Posts: n/a
#1: Jul 19 '05
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



Dimitris Kamenopoulos
Guest
 
Posts: n/a
#2: Jul 19 '05

re: NULL


Agent Mulder wrote:
[color=blue]
> 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?[/color]

NULL is almost certainly defined as
#define NULL 0

So why not? It's just substituted text.

John Harrison
Guest
 
Posts: n/a
#3: Jul 19 '05

re: NULL



"Dimitris Kamenopoulos" <dkamen4spam@cslab.ece.ntua.gr> wrote in message
news:bj8395$276p$1@ulysses.noc.ntua.gr...[color=blue]
> Agent Mulder wrote:
>[color=green]
> > 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?[/color]
>
> NULL is almost certainly defined as
> #define NULL 0
>
> So why not? It's just substituted text.
>[/color]

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


Ron Natalie
Guest
 
Posts: n/a
#4: Jul 19 '05

re: NULL



"Agent Mulder" <mbmulder_remove_this_@home.nl> wrote in message news:bj8320$ilm$1@news3.tilbu1.nb.home.nl...[color=blue]
> 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.
>[/color]
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.


jeffc
Guest
 
Posts: n/a
#5: Jul 19 '05

re: NULL



"John Harrison" <john_andronicus@hotmail.com> wrote in message
news:bj83it$gia7t$1@ID-196037.news.uni-berlin.de...[color=blue]
>[color=green][color=darkred]
>> >[/color]
> > NULL is almost certainly defined as
> > #define NULL 0
> >
> > So why not? It's just substituted text.
> >[/color]
>
> I think this illustrates what a waste of time NULL is. Its pretending to[/color]
be[color=blue]
> a special pointer value, but really it just zero. Don't pretend, use zero.[/color]

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?


Peter van Merkerk
Guest
 
Posts: n/a
#6: Jul 19 '05

re: NULL


> > > NULL is almost certainly defined as[color=blue][color=green][color=darkred]
> > > #define NULL 0
> > >
> > > So why not? It's just substituted text.
> > >[/color]
> >
> > I think this illustrates what a waste of time NULL is. Its pretending to[/color]
> be[color=green]
> > a special pointer value, but really it just zero. Don't pretend, use[/color][/color]
zero.[color=blue]
>
> 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?[/color]

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.

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl



White Wolf
Guest
 
Posts: n/a
#7: Jul 19 '05

re: NULL


Peter van Merkerk wrote:
[SNIP][color=blue][color=green]
>> #define ONE 1
>>
>> God, can you imagine?[/color]
>
> 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.[/color]

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.

--
WW aka Attila


Ron Natalie
Guest
 
Posts: n/a
#8: Jul 19 '05

re: NULL



"White Wolf" <wolof@freemail.hu> wrote in message news:bj888u$o3k$1@phys-news1.kolumbus.fi...[color=blue]
> Peter van Merkerk wrote:
> [SNIP][color=green][color=darkred]
> >> #define ONE 1
> >>
> >> God, can you imagine?[/color]
> >
> > 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.[/color]
>
> Hehe. A college of mine turned C into Korn shell. His code was something
> like this:
>
> if (..) then
> fi[/color]

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.



Agent Mulder
Guest
 
Posts: n/a
#9: Jul 19 '05

re: NULL


>> #define ONE 1[color=blue][color=green]
>>
>> God, can you imagine?[/color][/color]
[color=blue]
>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[/color]
some[color=blue]
>people really do love this kind of thing.[/color]

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

-X


Noah Roberts
Guest
 
Posts: n/a
#10: Jul 19 '05

re: NULL


John Harrison wrote:[color=blue]
> "Dimitris Kamenopoulos" <dkamen4spam@cslab.ece.ntua.gr> wrote in message
> news:bj8395$276p$1@ulysses.noc.ntua.gr...
>[color=green]
>>Agent Mulder wrote:
>>
>>[color=darkred]
>>>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?[/color]
>>
>>NULL is almost certainly defined as
>>#define NULL 0
>>
>>So why not? It's just substituted text.
>>[/color]
>
>
> 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.[/color]

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.
[color=blue]
>
> john
>
>[/color]


Rolf Magnus
Guest
 
Posts: n/a
#11: Jul 19 '05

re: NULL


Ron Natalie wrote:
[color=blue]
>
> "Agent Mulder" <mbmulder_remove_this_@home.nl> wrote in message
> news:bj8320$ilm$1@news3.tilbu1.nb.home.nl...[color=green]
>> 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.
>>[/color]
> 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.[/color]

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.

jeffc
Guest
 
Posts: n/a
#12: Jul 19 '05

re: NULL



"Noah Roberts" <nroberts@dontemailme.com> wrote in message
news:3F57A826.5090203@dontemailme.com...[color=blue]
>
> 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.[/color]

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.


Mike Smith
Guest
 
Posts: n/a
#13: Jul 19 '05

re: NULL


Agent Mulder wrote:
[color=blue]
> 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?[/color]

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.

--
Mike Smith

Ron Natalie
Guest
 
Posts: n/a
#14: Jul 19 '05

re: NULL



"Noah Roberts" <nroberts@dontemailme.com> wrote in message news:3F57A826.5090203@dontemailme.com...[color=blue]
> 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.
>[/color]
You can't ever change this. The value really is a zero.


Peter Ammon
Guest
 
Posts: n/a
#15: Jul 19 '05

re: NULL


John Harrison wrote:
[color=blue]
> "Dimitris Kamenopoulos" <dkamen4spam@cslab.ece.ntua.gr> wrote in message
> news:bj8395$276p$1@ulysses.noc.ntua.gr...
>[color=green]
>>Agent Mulder wrote:
>>
>>[color=darkred]
>>>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?[/color]
>>
>>NULL is almost certainly defined as
>>#define NULL 0
>>
>>So why not? It's just substituted text.
>>[/color]
>
>
> 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[/color]

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

Noah Roberts
Guest
 
Posts: n/a
#16: Jul 19 '05

re: NULL


jeffc wrote:[color=blue]
> "Noah Roberts" <nroberts@dontemailme.com> wrote in message
> news:3F57A826.5090203@dontemailme.com...
>[color=green]
>>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.[/color]
>
>
> 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.
>
>[/color]

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

Noah Roberts
Guest
 
Posts: n/a
#17: Jul 19 '05

re: NULL


Peter Ammon wrote:[color=blue]
> John Harrison wrote:
>[color=green]
>> "Dimitris Kamenopoulos" <dkamen4spam@cslab.ece.ntua.gr> wrote in message
>> news:bj8395$276p$1@ulysses.noc.ntua.gr...
>>[color=darkred]
>>> Agent Mulder wrote:
>>>
>>>
>>>> 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.
>>>[/color]
>>
>>
>> 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[/color]
>
>
> 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.[/color]

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

Kevin Goodsell
Guest
 
Posts: n/a
#18: Jul 19 '05

re: NULL


Noah Roberts wrote:
[color=blue]
>
> variable = 0;
>
> Is variable most likely a pointer or a simple variable?
>
> variable = NULL;
>
> Is variable most likely a pointer or a simple variable?
>[/color]

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 *)?

[color=blue]
> 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".[/color]

Any such information you gather may very well be wrong.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Kevin Goodsell
Guest
 
Posts: n/a
#19: Jul 19 '05

re: NULL


Noah Roberts wrote:
[color=blue]
> Peter Ammon wrote:
>[color=green]
>>
>>
>>
>> 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.[/color]
>
>
> 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).[/color]

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

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.
[color=blue]
>
> The same goes for true and false...false is 0, so why use it, why not
> just use 0 and 1?[/color]

Because the types are different, for one thing.

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

func(0);
func(false);

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jeremy Collins
Guest
 
Posts: n/a
#20: Jul 19 '05

re: NULL


jeffc wrote:
[color=blue]
> "Noah Roberts" <nroberts@dontemailme.com> wrote in message
> news:3F57A826.5090203@dontemailme.com...
>[color=green]
>>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.[/color]
>
>
> We hardly need that to tell us what it means. When I listen to soccer,
> English announcers say "nil" instead of "zero".[/color]

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


--
jc

Remove the -not from email

Ron Natalie
Guest
 
Posts: n/a
#21: Jul 19 '05

re: NULL



"Peter Ammon" <peter_ammon@rocketmail.com> wrote in message news:bj8rs0$ddk$1@news.apple.com...
[color=blue]
> The expression "someValue = 0" conveys to me that someValue is an
> arithmetic type,[/color]

What does someValue++ convey to you then?


jeffc
Guest
 
Posts: n/a
#22: Jul 19 '05

re: NULL



"Noah Roberts" <nroberts@dontemailme.com> wrote in message
news:3F57F335.8030200@dontemailme.com...[color=blue]
> 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".[/color]

Bad way to go about gathering info.


jeffc
Guest
 
Posts: n/a
#23: Jul 19 '05

re: NULL



"Peter Ammon" <peter_ammon@rocketmail.com> wrote in message
news:bj8rs0$ddk$1@news.apple.com...[color=blue]
>
> 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.[/color]

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

I don't think they should.


jeffc
Guest
 
Posts: n/a
#24: Jul 19 '05

re: NULL



"Noah Roberts" <nroberts@dontemailme.com> wrote in message
news:3F57F44F.9080900@dontemailme.com...[color=blue][color=green]
> >
> > 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.[/color]
>
> 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?[/color]

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.


Jakob Bieling
Guest
 
Posts: n/a
#25: Jul 19 '05

re: NULL


"Noah Roberts" <nroberts@dontemailme.com> wrote in message
news:3F57F335.8030200@dontemailme.com...[color=blue]
> jeffc wrote:[color=green]
> > "Noah Roberts" <nroberts@dontemailme.com> wrote in message
> > news:3F57A826.5090203@dontemailme.com...
> >[color=darkred]
> >>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.[/color]
> >
> >
> > 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.
> >
> >[/color]
>
> 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".[/color]


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

regards
--
jb

(replace y with x if you want to reply by e-mail)


Jakob Bieling
Guest
 
Posts: n/a
#26: Jul 19 '05

re: NULL


"jeffc" <nobody@nowhere.com> wrote in message
news:3f58a544_3@news1.prserv.net...
[color=blue]
> 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.[/color]


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
--
jb

(replace y with x if you want to reply by e-mail)


jeffc
Guest
 
Posts: n/a
#27: Jul 19 '05

re: NULL



"Jakob Bieling" <netsurf@gmy.net> wrote in message
news:bja92e$1s0$05$1@news.t-online.com...[color=blue]
> "jeffc" <nobody@nowhere.com> wrote in message
> news:3f58a544_3@news1.prserv.net...
>[color=green]
> > 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.[/color]
>
>
> I disagree in that it is not part of the language. Though I agree that
> there is room for improvement.[/color]

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

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


Ivan Vecerina
Guest
 
Posts: n/a
#28: Jul 19 '05

re: NULL


"Peter Ammon" <peter_ammon@rocketmail.com> wrote in message
news:bj8rs0$ddk$1@news.apple.com...
| 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
--
http://www.post1.com/~ivec <> Ivan Vecerina



Ron Natalie
Guest
 
Posts: n/a
#29: Jul 19 '05

re: NULL



"Ivan Vecerina" <ivec@myrealbox.com> wrote in message news:3f58cf3a$1@news.swissonline.ch...
[color=blue]
> 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.[/color]

You can't use 0. for null, but you can use '\0'. A char literal meets the
requirements of an integer constant expression.
[color=blue]
> 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.
>[/color]
Don't you confuse that with octal zero? :-)


Kevin Goodsell
Guest
 
Posts: n/a
#30: Jul 19 '05

re: NULL


jeffc wrote:
[color=blue]
>
>
> I don't understand. You are saying that NULL is part of the language?[/color]

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

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Kevin Goodsell
Guest
 
Posts: n/a
#31: Jul 19 '05

re: NULL


Ron Natalie wrote:
[color=blue]
> "Ivan Vecerina" <ivec@myrealbox.com> wrote in message news:3f58cf3a$1@news.swissonline.ch...
>
>[color=green]
>>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.[/color]
>
>
> You can't use 0. for null, but you can use '\0'. A char literal meets the
> requirements of an integer constant expression.
>[/color]

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
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jack Walker
Guest
 
Posts: n/a
#32: Jul 19 '05

re: NULL


Peter Ammon <peter_ammon@rocketmail.com> wrote in message news:<bj8rs0$ddk$1@news.apple.com>...[color=blue]
>
> 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[/color]

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
Andre Kostur
Guest
 
Posts: n/a
#33: Jul 19 '05

re: NULL


"Jakob Bieling" <netsurf@gmy.net> wrote in
news:bja8ik$146$01$1@news.t-online.com:
[color=blue][color=green]
>> 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".[/color]
>
>
> A reasonably decent IDE will tell you the type just by pointing at
> that
> variable name either by mouse or by cursor.[/color]

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.
Noah Roberts
Guest
 
Posts: n/a
#34: Jul 19 '05

re: NULL


Jeremy Collins wrote:[color=blue]
> 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.[/color]

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

NR

jeffc
Guest
 
Posts: n/a
#35: Jul 19 '05

re: NULL



"Andre Kostur" <nntpspam@kostur.net> wrote in message
news:Xns93ED8C0EBFF77nntpspamkosturnet@209.53.75.2 1...[color=blue]
>
> Doesn't help on a printout. I'm personally prefer using NULL to 0. I[/color]
like[color=blue]
> the extra reminder that I'm dealing with a pointer.[/color]

Really?
p = NULL;

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


Noah Roberts
Guest
 
Posts: n/a
#36: Jul 19 '05

re: NULL


jeffc wrote:[color=blue]
> "Andre Kostur" <nntpspam@kostur.net> wrote in message
> news:Xns93ED8C0EBFF77nntpspamkosturnet@209.53.75.2 1...
>[color=green]
>>Doesn't help on a printout. I'm personally prefer using NULL to 0. I[/color]
>
> like
>[color=green]
>>the extra reminder that I'm dealing with a pointer.[/color]
>
>
> Really?
> p = NULL;
>
> There, there's a "reminder" that you're using a pointer. Now, are you
> really?
>
>[/color]

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

Noah Roberts
Guest
 
Posts: n/a
#37: Jul 19 '05

re: NULL


Noah Roberts wrote:[color=blue]
> jeffc wrote:
>[color=green]
>> "Andre Kostur" <nntpspam@kostur.net> wrote in message
>> news:Xns93ED8C0EBFF77nntpspamkosturnet@209.53.75.2 1...
>>[color=darkred]
>>> Doesn't help on a printout. I'm personally prefer using NULL to 0. I[/color]
>>
>>
>> like
>>[color=darkred]
>>> the extra reminder that I'm dealing with a pointer.[/color]
>>
>>
>>
>> Really?
>> p = NULL;
>>
>> There, there's a "reminder" that you're using a pointer. Now, are you
>> really?
>>
>>[/color]
>
> 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
>[/color]

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


Noah Roberts
Guest
 
Posts: n/a
#38: Jul 19 '05

re: NULL


Noah Roberts wrote:[color=blue]
> 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
>
>[/color]

And another: http://cpptips.hyperformix.com/cpptips/null_templ

Bob Hairgrove
Guest
 
Posts: n/a
#39: Jul 19 '05

re: NULL


On Thu, 4 Sep 2003 21:22:41 +0200, "Agent Mulder"
<mbmulder_remove_this_@home.nl> wrote:
[color=blue]
>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?[/color]

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.


--
Bob Hairgrove
rhairgroveNoSpam@Pleasebigfoot.com
Ivan Vecerina
Guest
 
Posts: n/a
#40: Jul 19 '05

re: NULL


"Ron Natalie" <ron@sensor.com> wrote in message
news:3f58d43f$0$23224$9a6e19ea@news.newshosting.co m...[color=blue]
> "Ivan Vecerina" <ivec@myrealbox.com> wrote in message[/color]
news:3f58cf3a$1@news.swissonline.ch...[color=blue][color=green]
> > 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.[/color]
>
> You can't use 0. for null, but you can use '\0'. A char literal meets the
> requirements of an integer constant expression.[/color]
Agreed. But my point was just about NULL not having a type
specifically related to pointers.
[color=blue][color=green]
> > 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.
> >[/color]
> Don't you confuse that with octal zero? :-)[/color]

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
--
http://www.post1.com/~ivec <> Ivan Vecerina



Alexander Terekhov
Guest
 
Posts: n/a
#41: Jul 19 '05

re: NULL



Ivan Vecerina wrote:
[...][color=blue]
> I have yet to see 00 used to represent an integer value.[/color]

http://groups.google.com/groups?selm...AA0DA%40web.de
[color=blue]
> So using it to represent a null pointer value seems like
> a slight improvement over plain 0, in terms of expressiveness
> and readability.[/color]

00 is mine, end of story. Well, you'll have a better chance with 0p
(but see also my replies in the following thread):

http://google.com/groups?threadm=clt...31aj%404ax.com

regards,
alexander.
Ivan Vecerina
Guest
 
Posts: n/a
#42: Jul 19 '05

re: NULL


Hi Alexander!
"Alexander Terekhov" <terekhov@web.de> wrote in message
news:3F5A3D18.B14D2203@web.de...[color=blue]
>
> Ivan Vecerina wrote:
> [...][color=green]
> > I have yet to see 00 used to represent an integer value.[/color]
>
> http://groups.google.com/groups?selm...AA0DA%40web.de[/color]

The way you always refer to your previous posts amazes me - you
must be owning the depth record of self-referencing NG posts ;)
To quote the relevant few lines:
inline String::String( const String& other )
{
switch ( IntAtomicGet( other.data_->refs ) ) {
case 00: data_ = new StringBuf( *other.data_ ); break;
default: IntAtomicIncrement( (data_ = other.data_)->refs );
} ++nCopies;
}
Reading this, I'd have to ask: why a switch()? and why a 00?
I can guess it is supposed to label non-shareable strings ?
Have you considered using NULL in this case ? <grin>
[color=blue][color=green]
> > So using it to represent a null pointer value seems like
> > a slight improvement over plain 0, in terms of expressiveness
> > and readability.[/color]
>
> 00 is mine, end of story.[/color]

Well, at least I can show some prior art so far:
http://groups.google.com/groups?selm...swissonline.ch


Now maybe someone will also show me code that uses a variable
named OO, to make things even more confusing ;)

I admit my "I have yet to see" statement was too daring. I should have
at least included something about "a reasonable motivation to...".
[color=blue]
> Well, you'll have a better chance with 0p
> (but see also my replies in the following thread):
>[/color]
http://google.com/groups?threadm=clt...31aj%404ax.com

Indeed, a type-safe nil template similar to the one you use in
these examples can be implemented in C++ today.
I guess such a template may make sense all uses of
C-style casts ( i.e. (int*)0 ) have been banned.

Anyway...


Kind regards,
Ivan
--
http://www.post1.com/~ivec <> Ivan Vecerina



White Wolf
Guest
 
Posts: n/a
#43: Jul 19 '05

re: NULL


Alexander Terekhov wrote:[color=blue]
> Ivan Vecerina wrote:
> [...][color=green]
>> I have yet to see 00 used to represent an integer value.[/color]
>
> http://groups.google.com/groups?selm...AA0DA%40web.de
>[color=green]
>> So using it to represent a null pointer value seems like
>> a slight improvement over plain 0, in terms of expressiveness
>> and readability.[/color]
>
> 00 is mine, end of story.[/color]
[SNIP]

And where will all the others go? (00 in many cultures represent the same
concept as WC, toliet, restroom)

--
WW aka Attila


Alexander Terekhov
Guest
 
Posts: n/a
#44: Jul 19 '05

re: NULL



Ivan Vecerina wrote:
[...][color=blue]
> inline String::String( const String& other )
> {
> switch ( IntAtomicGet( other.data_->refs ) ) {
> case 00: data_ = new StringBuf( *other.data_ ); break;
> default: IntAtomicIncrement( (data_ = other.data_)->refs );
> } ++nCopies;
> }
> Reading this, I'd have to ask: why a switch()?[/color]

Copy&paste thing, to tell the truth.

http://groups.google.com/groups?selm...474F9%40web.de
[color=blue]
> and why a 00?[/color]

Looks good. Feels better. (WW can confirm ;-) )
[color=blue]
> I can guess it is supposed to label non-shareable strings ?[/color]

Yep.

regards,
alexander.
jeffc
Guest
 
Posts: n/a
#45: Jul 19 '05

re: NULL



"Noah Roberts" <nroberts@dontemailme.com> wrote in message
news:3F5913EF.2050807@dontemailme.com...[color=blue]
>
> 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[/color]

You can post all the definitions you want. You're missing the point that
they're all non-standard.


Peter Ammon
Guest
 
Posts: n/a
#46: Jul 19 '05

re: NULL


Ron Natalie wrote:
[color=blue]
> "Peter Ammon" <peter_ammon@rocketmail.com> wrote in message news:bj8rs0$ddk$1@news.apple.com...
>
>[color=green]
>>The expression "someValue = 0" conveys to me that someValue is an
>>arithmetic type,[/color]
>
>
> What does someValue++ convey to you then?[/color]

That someValue is a pointer other than a function pointer or void*, an
arithmetic type, or some type with the postincrement operator overloaded :>

-Peter

Ron Natalie
Guest
 
Posts: n/a
#47: Jul 19 '05

re: NULL



"Peter Ammon" <peter_ammon@rocketmail.com> wrote in message news:bjofem$66d$1@news.apple.com...[color=blue]
> Ron Natalie wrote:
>[color=green]
> > "Peter Ammon" <peter_ammon@rocketmail.com> wrote in message news:bj8rs0$ddk$1@news.apple.com...
> >
> >[color=darkred]
> >>The expression "someValue = 0" conveys to me that someValue is an
> >>arithmetic type,[/color]
> >
> >
> > What does someValue++ convey to you then?[/color]
>
> That someValue is a pointer other than a function pointer or void*, an
> arithmetic type, or some type with the postincrement operator overloaded :>
>[/color]
So why is this different than someValue = 0?


Agent Mulder
Guest
 
Posts: n/a
#48: Jul 19 '05

re: NULL


<Agent Mulder>[color=blue]
> int a=NULL;
> float b=NULL;
> etc.
> Is that valid?[/color]
</>

<Dimitris Kamenopoulos>
NULL is almost certainly defined as
#define NULL 0
So why not? It's just substituted text.
</>

I am reading from Windows 3.1 Guide to Programming, Microsoft Press,
that is both ancient and off-topic, chapter 14, C and Assembly Language:

<Microsoft Windows Guide to Programming>
14.2 Using the NULL Constant

Te symbolic constant NULL has different definitions for Windows and the
Microsoft C Optimizing Compiler (CL). The WINDOWS.H header file
defines NULL as follows:

#define NULL 0

On the other hand, the CL library header files (such as STDDEF.H) define
NULL as follows:

#ifndef NULL
#define NULL ((void *) 0 )
#endif

To avoid compiler warnings, you should use NULL only for pointers and
handles. You should not use NULL for such data types as int and WORD.
</>

-X


Kevin Goodsell
Guest
 
Posts: n/a
#49: Jul 19 '05

re: NULL


Agent Mulder wrote:
[color=blue]
> <Agent Mulder>
>[color=green]
>>int a=NULL;
>>float b=NULL;
>>etc.
>>Is that valid?[/color]
>
> </>
>
> <Dimitris Kamenopoulos>
> NULL is almost certainly defined as
> #define NULL 0
> So why not? It's just substituted text.
> </>
>
> I am reading from Windows 3.1 Guide to Programming, Microsoft Press,
> that is both ancient and off-topic, chapter 14, C and Assembly Language:
>
> <Microsoft Windows Guide to Programming>
> 14.2 Using the NULL Constant
>
> Te symbolic constant NULL has different definitions for Windows and the
> Microsoft C Optimizing Compiler[/color]

The *C* Optimizing Compiler. Not C++. C++ forbids this form of NULL. C
does not. Even if that were not the case, you can't take the way
Microsoft does things as standard.
[color=blue]
> #ifndef NULL
> #define NULL ((void *) 0 )
> #endif
>[/color]

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Closed Thread