Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Assigning NULL to a variable (not pointer)

Reply
Thread Tools

Assigning NULL to a variable (not pointer)

 
 
leonm54@gmail.com
Guest
Posts: n/a
 
      10-16-2006
I remember that this is a bad practice, but can not find a definitive
resource that states it is bad and why. Can anyone help?

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      10-16-2006
wrote:
> I remember that this is a bad practice, but can not find a definitive
> resource that states it is bad and why. Can anyone help?


<devilsadvocate>

What if nobody besides you think it's a bad practice? And shouldn't
the sheer fact that you can't find any definitive source stating it's
bad tell you that it might not be all that bad? Just asking...

</devilsadvocate>

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
 
Frederick Gotham
Guest
Posts: n/a
 
      10-16-2006
Leon posted:

> I remember that this is a bad practice, but can not find a definitive
> resource that states it is bad and why. Can anyone help?



Imagine two different planets with two totally different species, two
totally different cultures. These two planets don't communicate with each
other as they are unaware of each other's existance.

Give each of them the C++ Standard and a compiler.

One planet will come up with its own ideals and styles, and the other
planet will come up with different ones.

If your question is to do with C++ itself, then the answer is simply this:
Using NULL for zero is harmless, and your compiler won't complain.

If your question is to do with the culture of C++ which exists presently on
Earth, then the answer is: We tend to use NULL only for pointers... and
it's quite irregular to use NULL for integers.

So if you want to fit in, don't use NULL for integers.

If you want to use NULL for integer zero, then go ahead, your compiler
won't complain.

I myself never use NULL.

--

Frederick Gotham
 
Reply With Quote
 
=?ISO-8859-15?Q?Juli=E1n?= Albo
Guest
Posts: n/a
 
      10-16-2006
wrote:

> I remember that this is a bad practice, but can not find a definitive
> resource that states it is bad and why. Can anyone help?


NULL evaluates to a integer constant with value 0. If you want to assign 0,
using 0 is a lot more clear. And if you use 0 not by the numeric value but
as some error/no error flag, the following will be more expressive:

const int MyFuncNoError= 0;

int myfunc ()
{
// ...whatever
return MyFuncNoError;
}

if (myfunc () == MyFuncNoError)

Using NULL you just confuse people by making it to think that myfunc returns
some type of pointer.

Some people use NULL for '\0', probably confusing it with the NUL name of
the char in ascii. This also causes a lot of confusion:

if (somestr != NULL && * somestr != NULL)

Looking at this you can think that somestr is a char * * instead of a char
*.

In a sentence: NULL is intended to be used with pointers, if you use it to
other things you just add confusion and get no benefit.

--
Salu2
 
Reply With Quote
 
leonm54@gmail.com
Guest
Posts: n/a
 
      10-16-2006
First, thats for the replies to this query.

I thought I remembered that in some systems that NULL is not '\0'.
I know that in the majority of systems NULL can equate to '\0', but
I thought there were exceptions and did not what to make any
assumptions.

Not only that, I thought that the definition of NULL was (at least in
some instances): ((void *)0). If one attempted to assign that to an
integer, you would at least get a casting error.

If either the above statements are true, I would think that doing
something like "int var = NULL;" would generally be a bad idea.


Frederick Gotham wrote:
> Leon posted:
>
> > I remember that this is a bad practice, but can not find a definitive
> > resource that states it is bad and why. Can anyone help?

>
>
> Imagine two different planets with two totally different species, two
> totally different cultures. These two planets don't communicate with each
> other as they are unaware of each other's existance.
>
> Give each of them the C++ Standard and a compiler.
>
> One planet will come up with its own ideals and styles, and the other
> planet will come up with different ones.
>
> If your question is to do with C++ itself, then the answer is simply this:
> Using NULL for zero is harmless, and your compiler won't complain.
>
> If your question is to do with the culture of C++ which exists presently on
> Earth, then the answer is: We tend to use NULL only for pointers... and
> it's quite irregular to use NULL for integers.
>
> So if you want to fit in, don't use NULL for integers.
>
> If you want to use NULL for integer zero, then go ahead, your compiler
> won't complain.
>
> I myself never use NULL.
>
> --
>
> Frederick Gotham


 
Reply With Quote
 
Clark S. Cox III
Guest
Posts: n/a
 
      10-16-2006
wrote:

[First, please avoid top-posting]

> First, thats for the replies to this query.
>
> I thought I remembered that in some systems that NULL is not '\0'.
> I know that in the majority of systems NULL can equate to '\0', but
> I thought there were exceptions and did not what to make any
> assumptions.


In C++, NULL is *always* a constant integer expression with the value of
zero. The most common definition of NULL that I've seen is simply:

#define NULL 0

> Not only that, I thought that the definition of NULL was (at least in
> some instances): ((void *)0). If one attempted to assign that to an
> integer, you would at least get a casting error.


Not in C++. In C, it is legal for the implementation to define NULL as
(void*)0.

> If either the above statements are true, I would think that doing
> something like "int var = NULL;" would generally be a bad idea.


"int var = NULL;" *is* bad when considering the generally accepted
conventions. However, there is nothing in the language that makes "int
var = NULL;" illegal. Given that statement, any C++ compiler has to
initialize var to zero.


--
Clark S. Cox III

 
Reply With Quote
 
=?ISO-8859-15?Q?Juli=E1n?= Albo
Guest
Posts: n/a
 
      10-16-2006
wrote:

> I thought I remembered that in some systems that NULL is not '\0'.


NULL is never '\0', NULL is 0. If you assign 0 to a char the char gets the
value '\0' by means of habitual conversions, nothing related with NULL
itself.

> Not only that, I thought that the definition of NULL was (at least in
> some instances): ((void *)0).


Never in standard C++. Some C compilers do it that way.

--
Salu2
 
Reply With Quote
 
Clark S. Cox III
Guest
Posts: n/a
 
      10-16-2006
Julián Albo wrote:
> wrote:
>
>> I thought I remembered that in some systems that NULL is not '\0'.

>
> NULL is never '\0', NULL is 0. If you assign 0 to a char the char gets the
> value '\0' by means of habitual conversions, nothing related with NULL
> itself.


NULL could indeed be '\0', but that would have no effect on any
conforming program anyway.


--
Clark S. Cox III

 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      10-16-2006
wrote:
..
>
> I thought I remembered that in some systems that NULL is not '\0'.
> I know that in the majority of systems NULL can equate to '\0', but
> I thought there were exceptions and did not what to make any
> assumptions.


There's no reason why '\0' isn't a valid NULL.

>
> Not only that, I thought that the definition of NULL was (at least in
> some instances): ((void *)0). If one attempted to assign that to an
> integer, you would at least get a casting error.


It can be that in C, it's not allowed in C++.
 
Reply With Quote
 
ZVFJSMKBROWI@spammotel.com
Guest
Posts: n/a
 
      10-16-2006
First, again thank you for your posts.

I believe I got the point about NULL.

Now I ask a favor. I made the mistake of using my GMAIL account to
setup my Google Groups account and the bloody thing dumped my email
address in the postings. I have since taken care of the issue for my
posting, but the responses posted still contain my email. Could you
please delete your postings so that the email sniffers won't find me?
Thank you in advance.

LM


Clark S. Cox III wrote:
> Julián Albo wrote:
> > wrote:
> >
> >> I thought I remembered that in some systems that NULL is not '\0'.

> >
> > NULL is never '\0', NULL is 0. If you assign 0 to a char the char gets the
> > value '\0' by means of habitual conversions, nothing related with NULL
> > itself.

>
> NULL could indeed be '\0', but that would have no effect on any
> conforming program anyway.
>
>
> --
> Clark S. Cox III
>


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
assigning pointer to NULL Ian Collins C Programming 27 02-10-2008 10:35 PM
Assigning methods to objects, and assigning onreadystatechange to an XMLHttpRequest -- an inconsistency? weston Javascript 1 09-22-2006 09:33 AM
"stringObj == null" vs "stringObj.equals(null)", for null check?? qazmlp1209@rediffmail.com Java 5 03-29-2006 10:37 PM
if instance variable get initialize after assigning some values or after constructor then when does static variable get initialize Tony Morris Java 3 02-04-2006 08:39 AM
Assigning value to null Steve Caliendo ASP .Net 1 06-08-2004 04:41 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57