not null

J

John J

The following code is complete and working within a class; however, it uses
"e" as a pointer to an Entry object without checking if that it is not null.
This of course doesn't matter if objects are created correctly; however, it
will probably cause a problem should the "e" be null. Is there a simple
means I can use to check that "e" is not null?



Thanks for any help





//Find the winner

void Race::winner (ostream& out) const

{

Entry* e = NULL;

if (nEntries == 0)

{

out << "No entries" << endl;

}



else

{

bool found_a_winner = false; // no winner found
yet



for (int i=0; i<nEntries; i++)

{

e = entries;

if (e->getPlace() == 1)

{

out << "The winner is: " << endl;

out << *e;

found_a_winner = true; // a winner has been
found

}

}



if (!found_a_winner) // if no winner

{

out << "No winner was found in this
race" << endl;

}

}

}
 
J

John Harrison

John J said:
The following code is complete and working within a class; however, it uses
"e" as a pointer to an Entry object without checking if that it is not null.
This of course doesn't matter if objects are created correctly; however, it
will probably cause a problem should the "e" be null. Is there a simple
means I can use to check that "e" is not null?

Yes the code you had before

if (e)
{
// e is not null
}
else
{
// e is null
}

was perfectly correct. However I would say this isn't the right place to
test for it. It is when the object is created that you should be checking if
it is created correctly. The way you can report any error as soon as it
happens.

john
 
J

Jacques Labuschagne

John said:
if (e)
{
// e is not null

Or even
if (e != NULL){
...
}
which is of course equivalent to (and clearer than)
if (e != 0){
...
}

Regards,
Jacques.
 
B

Bill Seurer

Jacques said:
Or even
if (e != NULL){
...
}
which is of course equivalent to (and clearer than)
if (e != 0){
...
}

NULL is from some of the C includes and is not a base part of the
language. 0 is correct.
 
D

David Rubin

John said:
The following code is complete and working within a class; however, it uses
"e" as a pointer to an Entry object without checking if that it is not null.
This of course doesn't matter if objects are created correctly; however, it
will probably cause a problem should the "e" be null. Is there a simple
means I can use to check that "e" is not null?
[snip]
void Race::winner (ostream& out) const

{

Entry* e = NULL;
[snip - get e from an array of pointers and use it]

As John Harrison indicated, this is not the right place to validate e.
However, if it is "impossible by design" for e to be null at this point
in your program, you should assert() or throw an exception to indicate
that something has gone drastically wrong.

/david
 
R

Rolf Magnus

Bill said:
NULL is from some of the C includes and is not a base part of the
language.

The standard C headers are a base part of the C++ language, just like
the C++ headers are.
0 is correct.

NULL is also correct (if you #include <cstdlib> oder <stdlib.h>), but 0
is to be preferred.
 
B

Buster

Rolf said:
The standard C headers are a base part of the C++ language, just like
the C++ headers are.

No. The language is one thing, the libraries another,
even though both are defined by the standard.
NULL is also correct (if you #include <cstdlib> oder <stdlib.h>), but 0
is to be preferred.

Agreed. There are other headers that define NULL too.
 
J

Julie

Bill said:
NULL is from some of the C includes and is not a base part of the
language. 0 is correct.

So is your point that NULL is not part of C++???

Who is this mystery person/committee that can dictate to me what is correct and
clearer? In my experience, comparing pointers to 0 is _not_ more correct or
clearer than comparing to NULL. NULL is much more self-documenting, and much
more cleanly conveys the intent over comparing to 0.
 
B

Buster

Julie said:
Who is this mystery person/committee that can dictate to me what is correct and
clearer?

That straw man, over there in the next field.
In my experience, comparing pointers to 0 is _not_ more correct or
clearer than comparing to NULL. NULL is much more self-documenting, and much
more cleanly conveys the intent over comparing to 0.

Either you mean "In my opinion", or you're the despot. I like '0'.
 
B

Bill Seurer

Julie said:
So is your point that NULL is not part of C++???

It is part of some of the header files.
Who is this mystery person/committee that can dictate to me what is correct and
clearer? In my experience, comparing pointers to 0 is _not_ more correct or
clearer than comparing to NULL. NULL is much more self-documenting, and much
more cleanly conveys the intent over comparing to 0.

If this compiles your C++ compiler is broken:


int main() {
int i;
int *p = &i;
if (p == NULL)
return 1;
return 0;
}


Having to include some header file, especially one of the C library
ones, just to get a simple program that does a trivial comparison to
compile is not good.
 
B

Bill Seurer

Bill said:
It is part of some of the header files.



If this compiles your C++ compiler is broken:


int main() {
int i;
int *p = &i;
if (p == NULL)
return 1;
return 0;
}


Having to include some header file, especially one of the C library
ones, just to get a simple program that does a trivial comparison to
compile is not good.

P.S. Read section 5.1.1 of _The C++ Programming Language_ by Stroustrup.
 
J

Julie

Buster said:
That straw man, over there in the next field.


Either you mean "In my opinion", or you're the despot. I like '0'.

No, I mean "in my experience", and I'm not the despot.

Do you feel that comparing to zero is more _self_documenting_ than comparing to
NULL irrespective of your preference?

How do you conversationally discuss such pointers? Do you say: 'the pointer is
null' or 'the pointer is zero', or something else?
 
B

Buster

Julie said:
Buster wrote:

No, I mean "in my experience", and I'm not the despot.

Do you feel that comparing to zero is more _self_documenting_ than comparing to
NULL irrespective of your preference?

Neither more nor less, in my opinion.
How do you conversationally discuss such pointers? Do you say: 'the pointer is
null' or 'the pointer is zero', or something else?

The first, of course. Note that "null" is an adjective.
 
K

Karl Heinz Buchegger

Bill said:
It is part of some of the header files.


If this compiles your C++ compiler is broken:

int main() {
int i;
int *p = &i;
if (p == NULL)
return 1;
return 0;
}

Having to include some header file, especially one of the C library
ones, just to get a simple program that does a trivial comparison to
compile is not good.

That's a moot argumentation.
Every real world program includes some system headers.

I could use your argumentation against std::cout also.

The real reason why 0 is preferred in C++ is that NULL is an integer
and not a pointer type. This may lead to suprising results in case
of overloaded functions, when suddenly the int version is called and
not the pointer version, as may be expected by someone.
Having said that: I prefer using NULL too, knowing the possible
pitfall.
 
J

Julie

Karl said:
That's a moot argumentation.
Every real world program includes some system headers.

I could use your argumentation against std::cout also.

The real reason why 0 is preferred in C++ is that NULL is an integer
and not a pointer type. This may lead to suprising results in case
of overloaded functions, when suddenly the int version is called and
not the pointer version, as may be expected by someone.
Having said that: I prefer using NULL too, knowing the possible
pitfall.

Has there ever been any discussion of adding NULL to the base language (not as
a manifest constant) where it specifically _is_ a pointer type?
 
R

Rolf Magnus

Julie said:
Has there ever been any discussion of adding NULL to the base language
(not as a manifest constant) where it specifically _is_ a pointer
type?

Which pointer type would it be then?
 
J

JKop

Rolf Magnus posted:
Which pointer type would it be then?


LMAO!!!

This conversation is so stupid!!!


if (number != 0) //This is pidgeon C++


if (number) //This is proper!


Similarly:

void Grent(unsigned int* pK)
{
if (!pK) throw "ahhh!!!!!";
}


---

For Clarity, an "if", "while", "for" statement will execute if the value is
true.

True means "NOT EQUAL TO 0". -5 is true, 's' is true.

False means "EQUAL TO 0". 0 is false. '\0' is false. 0.0 is false. 0x0 is
false. 00 is false.


-JKop
 
J

Julie

Rolf said:
Which pointer type would it be then?

I don't know, something generic and typeless that can be applied (and only
applied) to anything * (void * on up).
 
J

Juergen Heinzl

So is your point that NULL is not part of C++???
[-]
It's neither part of the C nor of the C++ language. If you
want it you ...

C++ : #include <cstddef> [see ISO-IEC-14882 : "18.1 Types"]
C : #include <stddef.h>

.... either need to include some header file or define your own version.

Unfortunately 0 instead of NULL isn't the final solution for all
cases either, as if you've something like this ...

class C {
public :
void f( int );
void f( C* );
};

... you'd need something like this ...

class C {
public :
void f( int );
void f( C* );

static C* const NULL_;
};

C* const C::NULL_ = 0;

... in order to be able to write ...

some_c.f( C::NULL_ );

... instead of the more verbose version ...

some_c.f( static_cast<C*>(0) );

... if the 2nd f() function is to be called.

Ta',
Juergen
 

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

Similar Threads


Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top