Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > A question about NULL

Reply
Thread Tools

A question about NULL

 
 
Dmitry D
Guest
Posts: n/a
 
      08-16-2003
Hi,
I'm new to C++ (started learning in the beginning of this summer), and I
have the following question (sorry if it sounds stupid):
In many code samples and source files, I see NULL expression being used (for
example, int* someInt=NULL; ). I used similar initialization myself, and it
works fine even if I don't define NULL. But what is "NULL" exactly? Is it a
constant defined by compiler? Is there any difference between the following
two ways to initialize a pointer?
// example 1
int * myInt = NULL;

// example 2
int * myInt = 0;

If there is no difference, why would I use NULL instead of 0?
Thanks in advance.

Dmitry
P.S. This is my first post in the newsgroup


 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      08-16-2003
Dmitry D wrote:
> Hi,
> I'm new to C++ (started learning in the beginning of this summer), and I
> have the following question (sorry if it sounds stupid):
> In many code samples and source files, I see NULL expression being used (for
> example, int* someInt=NULL; ). I used similar initialization myself, and it
> works fine even if I don't define NULL. But what is "NULL" exactly? Is it a
> constant defined by compiler? Is there any difference between the following
> two ways to initialize a pointer?
> // example 1
> int * myInt = NULL;
>
> // example 2
> int * myInt = 0;
>
> If there is no difference, why would I use NULL instead of 0?
> Thanks in advance.
>
> Dmitry
> P.S. This is my first post in the newsgroup


NULL is usually a macro

#define NULL 0

and sometimes (but only for C as C++

#define NULL ((void *) 0)


'0' is a special case in the sense that

int * ptr = 0; // happy compiler

Is perfectly legal ... but

int * ptr = 1; // Illegal - cranky compiler

error: invalid conversion from `int' to `int*'


Why NULL? Some would argue readability. Some would argue that NULL is
not neccassarily '0', for example I *could* (but I would never) define
NULL and ((void *) 1).

NULL is usually defined in one of the standard header files.


 
Reply With Quote
 
 
 
 
Tim
Guest
Posts: n/a
 
      08-16-2003

"Dmitry D" <(E-Mail Removed)> wrote in message
newsvk%a.63871$(E-Mail Removed) ...
> Hi,
> I'm new to C++ (started learning in the beginning of this summer), and I
> have the following question (sorry if it sounds stupid):
> In many code samples and source files, I see NULL expression being used

(for
> example, int* someInt=NULL; ). I used similar initialization myself, and

it
> works fine even if I don't define NULL. But what is "NULL" exactly? Is it

a
> constant defined by compiler? Is there any difference between the

following
> two ways to initialize a pointer?
> // example 1
> int * myInt = NULL;
>
> // example 2
> int * myInt = 0;


In 'C', NULL is defined like this:

#define NULL (void*)0


In C++, NULL is defined like this:

#define NULL 0


If you try and initialise a pointer to a type with a pointer to void, you'll
get errors with a C++ compiler:

int* p = (void*)0; // OK in 'C*, error in C++
int* p = 0; // OK in C++, error in 'C'


C++ specifically forbids the 'C' style definition of NULL. Why? Perhaps
assigning a 'pointer to a type' to the 'C' style NULL could be considered
similar to assigning a 'pointer to a derived type' to a 'pointer to base
type', which would be wrong. Then again, is assigning a 'pointer to a type'
to the value zero is any more correct; besides which 'void' is not the base
type of all types? You could say that it's a bit of shambles (I do). Then
again, C++ isn't a real OO language; it's a baggage of OO extensions to 'C'
which is, essentially, just portable assembler. To do the job properly, you
would need a language that had a common base type for all types and Null
instances of every derived type.


 
Reply With Quote
 
Rob Williscroft
Guest
Posts: n/a
 
      08-16-2003
Dmitry D wrote in newsvk%a.63871$(E-Mail Removed) :

> Hi,
> I'm new to C++ (started learning in the beginning of this summer), and
> I have the following question (sorry if it sounds stupid):
> In many code samples and source files, I see NULL expression being
> used (for example, int* someInt=NULL; ). I used similar initialization
> myself, and it works fine even if I don't define NULL. But what is
> "NULL" exactly? Is it a constant defined by compiler? Is there any
> difference between the following two ways to initialize a pointer?
> // example 1
> int * myInt = NULL;
>
> // example 2
> int * myInt = 0;
>
> If there is no difference, why would I use NULL instead of 0?
> Thanks in advance.
>


NULL is a #define's macro to use it you have to include one of
the system header's that defines it, but don't use it.

In C++ NULL is usually defined to be 0 or 0L, so there is no
difference in the 2 examples you gave.

In C NULL is usually defined to be ((void *)0), this is more
"typesafe" in C.

So the macro NULL is useful if you have some code that is going to
bee seen by a C compiler and a C++ compiler, otherwise use 0 for C++.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
 
Reply With Quote
 
Tim
Guest
Posts: n/a
 
      08-16-2003

"Rolf Magnus" <(E-Mail Removed)> wrote in message
news:bhkv82$blb$06$(E-Mail Removed)-online.com...
> Rob Williscroft wrote:
>
> >> Hi,
> >> I'm new to C++ (started learning in the beginning of this summer),
> >> and I have the following question (sorry if it sounds stupid):
> >> In many code samples and source files, I see NULL expression being
> >> used (for example, int* someInt=NULL; ). I used similar
> >> initialization myself, and it works fine even if I don't define NULL.
> >> But what is "NULL" exactly? Is it a constant defined by compiler? Is
> >> there any difference between the following two ways to initialize a
> >> pointer? // example 1
> >> int * myInt = NULL;
> >>
> >> // example 2
> >> int * myInt = 0;
> >>
> >> If there is no difference, why would I use NULL instead of 0?
> >> Thanks in advance.
> >>

> >
> > NULL is a #define's macro to use it you have to include one of
> > the system header's that defines it, but don't use it.
> >
> > In C++ NULL is usually defined to be 0 or 0L, so there is no
> > difference in the 2 examples you gave.
> >
> > In C NULL is usually defined to be ((void *)0), this is more
> > "typesafe" in C.

>
> Actually, it's not. It just fools the user into thinking that it's more
> typesafe.


Is that true? Surely it 'really' is safer to initialise a pointer to type
with a ((void*)0); for instance, what about x86 segmented architecture;
there 0 and (void*(0)) really are quite different animals (only the offset
part of the pointer is zero).

> > So the macro NULL is useful if you have some code that is going to
> > bee seen by a C compiler and a C++ compiler, otherwise use 0 for C++.

>
> There is no reason to use NULL in C either.
>


....but it makes the intention clear, namely that you're at least
initialising a pointer to type with a pointer, not an integer.


 
Reply With Quote
 
Kevin Goodsell
Guest
Posts: n/a
 
      08-16-2003
Tim wrote:

>
> In 'C', NULL is defined like this:
>
> #define NULL (void*)0
>
>
> In C++, NULL is defined like this:
>
> #define NULL 0
>
>


NULL *may* be defined that way in C. It may also look like your second
example. Both are acceptable in C, but only the second is acceptable in
C++. However, it's not the *only* thing that's acceptable in C++. These
are also legal definitions of NULL in C++:

#define NULL 0L
#define NULL (1 - 1)
#define NULL !1
#define NULL !!0
#define NULL !!!!!!!!!!!!!!!!!!!!!!!!!!!1

In C, I use NULL to make it more obvious that I'm dealing with pointers.
In C++ that can be a dangerous way of thinking, because NULL is *not*,
in fact, a pointer. So if you have an overloaded function:

void f(long i);
void f(void *p);

And you attempt to call f(void *) this way:

f(NULL);

You will call the wrong function. NULL can lead you to believe you are
using a pointer when, if fact, you are not.

Technically, NULL can also be changed in a program:

#undef NULL
#define NULL 27

Which could cause serious problems. This is another reason to avoid it
and use 0 instead, but I think this one is a bit of a stretch. I can't
imagine why someone would do something like that.

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

 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      08-16-2003
On 16 Aug 2003 08:17:09 GMT, Gianni Mariani <(E-Mail Removed)>
wrote in comp.lang.c++:

> Dmitry D wrote:
> > Hi,
> > I'm new to C++ (started learning in the beginning of this summer), and I
> > have the following question (sorry if it sounds stupid):
> > In many code samples and source files, I see NULL expression being used (for
> > example, int* someInt=NULL; ). I used similar initialization myself, and it
> > works fine even if I don't define NULL. But what is "NULL" exactly? Is it a
> > constant defined by compiler? Is there any difference between the following
> > two ways to initialize a pointer?
> > // example 1
> > int * myInt = NULL;
> >
> > // example 2
> > int * myInt = 0;
> >
> > If there is no difference, why would I use NULL instead of 0?
> > Thanks in advance.
> >
> > Dmitry
> > P.S. This is my first post in the newsgroup

>
> NULL is usually a macro


Actually the C and C++ standards both require that NULL is a macro.

> #define NULL 0
>
> and sometimes (but only for C as C++
>
> #define NULL ((void *) 0)
>
>
> '0' is a special case in the sense that
>
> int * ptr = 0; // happy compiler
>
> Is perfectly legal ... but
>
> int * ptr = 1; // Illegal - cranky compiler
>
> error: invalid conversion from `int' to `int*'
>
>
> Why NULL? Some would argue readability. Some would argue that NULL is
> not neccassarily '0', for example I *could* (but I would never) define
> NULL and ((void *) 1).


NULL, at least in C++, is necessarily 0, although it may not be the
literal character constant '0'. The C++ language standard requires
that the macro NULL expand to an integer constant expression that
evaluates to 0.

So it could be:

#define NULL (42-4*7)

....but it can't be defined as ((void *)1). In fact it can't be
defined as a pointer at all in C++. It can in C, but only (again) as
an integer constant expression that evaluates to 0 being cast to a
pointer to void.

> NULL is usually defined in one of the standard header files.


NULL is required to be defined in several of the standard header
files.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
 
Reply With Quote
 
Jack Klein
Guest
Posts: n/a
 
      08-16-2003
On Sat, 16 Aug 2003 20:46:52 +0200, "Tim" <(E-Mail Removed)> wrote
in comp.lang.c++:

>
> "Rolf Magnus" <(E-Mail Removed)> wrote in message
> news:bhkv82$blb$06$(E-Mail Removed)-online.com...
> > Rob Williscroft wrote:
> >
> > >> Hi,
> > >> I'm new to C++ (started learning in the beginning of this summer),
> > >> and I have the following question (sorry if it sounds stupid):
> > >> In many code samples and source files, I see NULL expression being
> > >> used (for example, int* someInt=NULL; ). I used similar
> > >> initialization myself, and it works fine even if I don't define NULL.
> > >> But what is "NULL" exactly? Is it a constant defined by compiler? Is
> > >> there any difference between the following two ways to initialize a
> > >> pointer? // example 1
> > >> int * myInt = NULL;
> > >>
> > >> // example 2
> > >> int * myInt = 0;
> > >>
> > >> If there is no difference, why would I use NULL instead of 0?
> > >> Thanks in advance.
> > >>
> > >
> > > NULL is a #define's macro to use it you have to include one of
> > > the system header's that defines it, but don't use it.
> > >
> > > In C++ NULL is usually defined to be 0 or 0L, so there is no
> > > difference in the 2 examples you gave.
> > >
> > > In C NULL is usually defined to be ((void *)0), this is more
> > > "typesafe" in C.

> >
> > Actually, it's not. It just fools the user into thinking that it's more
> > typesafe.

>
> Is that true? Surely it 'really' is safer to initialise a pointer to type
> with a ((void*)0); for instance, what about x86 segmented architecture;
> there 0 and (void*(0)) really are quite different animals (only the offset
> part of the pointer is zero).


You have some misunderstandings here. First, C++ disallowed the C
definition of NULL as (void *)0 because it runs afoul of the typing
system. The following is legal C but illegal C++:

char *cp = (void *)0;

C allows conversion to and from void pointer to any object type
without a cast, C++ only allows the conversion from pointer to object
to void pointer, not the other way.

Second, perhaps you misunderstand something else. An integer constant
expression that evaluates to 0 is "magic" in C and C++ when used in a
pointer context. It makes no difference what the size of a pointer
is, or whether the architecture is segmented or has other unusual
quirks.

The expression:

type *type_pointer = 0;

....sets the pointer to a null pointer. That does not mean that a null
pointer has all bits 0 in its physical representation.

The compiler must recognize the use of such an expression at compile
time, and convert it to whatever the implementation uses internally as
an actual null pointer value.

Note that this only applies to compile time constant expressions. The
following:

int x = 0;
char *cp;
cp = x;

....is not guaranteed to make cp a null pointer.

Compilers have been required to perform this recognition and special
handling for constant 0 in a pointer context for at least 30 years
now, long before K&R1, long before the first ANSI C standard, and long
before Bjarne started extending C into C++.

>
> > > So the macro NULL is useful if you have some code that is going to
> > > bee seen by a C compiler and a C++ compiler, otherwise use 0 for C++.

> >
> > There is no reason to use NULL in C either.
> >

>
> ...but it makes the intention clear, namely that you're at least
> initialising a pointer to type with a pointer, not an integer.


This is the most important point. The three most common expressions
for 0 in C++ are 0, '\0', and NULL.

You could write:

char *cp = '\0'; // '\0' is a perfectly valid null pointer constant!

You could also write:

char ca [100];
ca [99] = NULL;

....but you wouldn't last long working on any project I was running!

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
 
Reply With Quote
 
Artie Gold
Guest
Posts: n/a
 
      08-16-2003
Jack Klein wrote:
[snip]
>
>
> NULL, at least in C++, is necessarily 0, although it may not be the
> literal character constant '0'. The C++ language standard requires
> that the macro NULL expand to an integer constant expression that
> evaluates to 0.
>
> So it could be:
>
> #define NULL (42-4*7)


OK Jack. It's Saturday. Step away from the keyboard. Easy now...



--ag
--
Artie Gold -- Austin, Texas

 
Reply With Quote
 
Samuel Barber
Guest
Posts: n/a
 
      08-16-2003
Rob Williscroft <(E-Mail Removed)> wrote in message news:<Xns93D95EC6D19E9ukcoREMOVEfreenetrtw@195.129 .110.200>...
> NULL is a #define's macro to use it you have to include one of
> the system header's that defines it, but don't use it.
>
> In C++ NULL is usually defined to be 0 or 0L, so there is no
> difference in the 2 examples you gave.
>
> In C NULL is usually defined to be ((void *)0), this is more
> "typesafe" in C.
>
> So the macro NULL is useful if you have some code that is going to
> bee seen by a C compiler and a C++ compiler, otherwise use 0 for C++.


Your conclusion doesn't follow. How is it "useful" to have the
definition of NULL change? All it can do is introduce bugs.

For example:

printf("%p", NULL);

is legal if NULL=(void*)0, but a bug if NULL=0.

Sam
 
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
createImage sometime returns null and sometime returns non-null. vizlab Java 3 10-17-2007 11:21 AM
"stringObj == null" vs "stringObj.equals(null)", for null check?? qazmlp1209@rediffmail.com Java 5 03-29-2006 10:37 PM
difference between null object and null string gokul.b@gmail.com Java 16 10-12-2005 06:43 PM
VB.NET Null to SQL Null (ASP.NET 2.0 GridView) Kivak Wolf ASP .Net 2 06-28-2005 02:01 PM
Is there a null ostream (like /dev/null) in cpp? Bo Peng C++ 13 07-18-2004 07:17 PM



Advertisments