isNull() method a good idea?

  • Thread starter Generic Usenet Account
  • Start date
G

Generic Usenet Account

I am going through some legacy code that has an "isNull()" method
defined on certain classes. I can see that this can be a good way to
eliminate certain types of crashes, by making this the first call in a
method (and bailing out immediately if it is true). However, if this
is such a good idea, why is it not common industry practice?

Mohan



/////////////// Sample source code //////////////
#include <iostream>

using namespace std;

class SampleClass
{
public:
SampleClass(int i):attrVal(i) {}
~SampleClass(){}

void printIt()
{
if(!isNull())
cout << "My attribute value is " << attrVal << endl;
else
cout << "I am NULL" << endl;
}

bool isNull() {return this == 0;}

protected:
int attrVal;
};



main()
{
SampleClass *ptr = NULL;

ptr->printIt();
}
 
J

Jonathan Mcdougall

Generic said:
I am going through some legacy code that has an "isNull()" method
defined on certain classes. I can see that this can be a good way to
eliminate certain types of crashes, by making this the first call in a
method (and bailing out immediately if it is true). However, if this
is such a good idea, why is it not common industry practice?

If you are talking about the code below, it is illegal in C++.
/////////////// Sample source code //////////////
#include <iostream>

using namespace std;

If this is a header file, don't do that. I don't want your using
directive if I include your file.
class SampleClass
{
public:
SampleClass(int i):attrVal(i) {}
~SampleClass(){}

void printIt()
{
if(!isNull())
cout << "My attribute value is " << attrVal << endl;
else
cout << "I am NULL" << endl;
}

bool isNull() {return this == 0;}

This will always return false in a conforming C++ program. You cannot
call a member function on a null poiner.
protected:
int attrVal;
};



main()

main() returns an int. Always.

int main()
{
SampleClass *ptr = NULL;

ptr->printIt();

Undefined behavior. Illegal. Bad for your health. Don't do that.


Jonathan
 
V

Victor Bazarov

Generic said:
I am going through some legacy code that has an "isNull()" method
defined on certain classes. I can see that this can be a good way to
eliminate certain types of crashes,

Eliminating certain types of crashes is a "good way" to hide other bugs,
which actually should be eliminated themselves instead.
> by making this the first call in a
method (and bailing out immediately if it is true). However, if this
is such a good idea, why is it not common industry practice?

No, it's not a good idea. I actually think it's A BAD IDEA(tm). Never
should a member function be called through a pointer that is null. It
causes undefined behaviour according to the language Standard.

V
 
I

Ian

Generic said:
I am going through some legacy code that has an "isNull()" method
defined on certain classes. I can see that this can be a good way to
eliminate certain types of crashes, by making this the first call in a
method (and bailing out immediately if it is true). However, if this
is such a good idea, why is it not common industry practice?
Worse than that, it's a stupid and dangerous idea.

It an object is NULL, how could this method ever be called?

Ian
 
G

Generic Usenet Account

Ian said:
It an object is NULL, how could this method ever be called?

Ian

I was myself very surprised to see the code executing (g++ and native
Solaris compiler)!

Mohan
 
L

Larry I Smith

Generic said:
I was myself very surprised to see the code executing (g++ and native
Solaris compiler)!

Mohan

It won't work as written in your post. There must be more to it -
IsNull() is a static member perhaps, or IsNull() used by a class
to check whther or not one of the class member variables is
NULL, etc...

Provide the actual code (class def's and usage) that you've seen
working.

Larry
 
V

Victor Bazarov

Larry said:
It won't work as written in your post.

Yes, it does "work". It is undefined behaviour nonetheless. So, whatever
it does, it does it by accident, AFA C++ is concerned. It is expected on
some operating systems, however. It used to be the rule among novice C++
programmers on MS-DOS, for example, to check if 'this' was 'NULL'...
> There must be more to it -
IsNull() is a static member perhaps, or IsNull() used by a class
to check whther or not one of the class member variables is
NULL, etc...

No, it isn't.
Provide the actual code (class def's and usage) that you've seen
working.

I just compiled it and saw it "working" on one of our Sun Enterprise 4000
machines with the source code "GUA" posted in the original post. Do not
guess, find a Sun machine and check it. What the hell, check it on your
Linux box. How long would it take you to copy-paste it and compile and
run the a.out?

V
 
I

Ian

Generic said:
Ian wrote:




I was myself very surprised to see the code executing (g++ and native
Solaris compiler)!
Oh it will execute, provided isNull() is a non-virtual member. All you
end up with is a function like

mangled_name_isNull( SampleClass* this )

being called with this == NULL.

But in anywhere other than a trivial example, you are asking for trouble.

If you are going to check for a null object, do it when the object is
created, or first encountered.

Ian
 
D

David White

Larry said:
It won't work as written in your post.

I think that an actual compilation of this is highly likely to work.
There must be more to it -
IsNull() is a static member perhaps,

It can't be. It refers to 'this'.
or IsNull() used by a class
to check whther or not one of the class member variables is
NULL, etc...

No, it detects whether 'this' is null, i.e., whether the caller is being
naughty.
This is what the MFC does in VC++ 6.0 (compiler vendor's privilege, I
suppose)
void CObject::AssertValid() const
{
ASSERT(this != NULL);
}
Provide the actual code (class def's and usage) that you've seen
working.

The OP has already done that.

The idea behind this is torpedoed by the language standard, as others have
pointed out. However, would a member function compiled by a real compiler
ever need to execute code dereferencing a null 'this' unless it is used to
access a member variable or call a virtual function? Probably not, and if
not, then the code would probably work, even though the standard says
nothing of the sort.

I think it's a bad idea, too, BTW, in case I've given the opposite
impression.

DW
 
P

Phil Staite

Victor said:
I just compiled it and saw it "working" on one of our Sun Enterprise 4000
machines with the source code "GUA" posted in the original post. Do not
guess, find a Sun machine and check it. What the hell, check it on your
Linux box. How long would it take you to copy-paste it and compile and
run the a.out?

This very thing came up a few weeks ago and it also works fine on SGI
systems. (for better or worse, you decide)

For "the usual" implementations it will work provided no virtual
functions are called nor any instance data accessed. Not sure if
multiple inheritance would hose this up or not and I'm too tired to
think about it just now ;-)

IMHO isNull() would only be useful a) during debug and b) if it dumped
source info to say cerr or a log file, then aborted the program. NULL
pointer errors are best caught and dealt with immediately during
development, never, ever handled quietly... (with the possible exception
of life or death code in a production/use environment where
failure/dumping is not an option)
 
M

Mirek Fidler

I am going through some legacy code that has an "isNull()" method
defined on certain classes. I can see that this can be a good way to
eliminate certain types of crashes, by making this the first call in a
method (and bailing out immediately if it is true). However, if this
is such a good idea, why is it not common industry practice?

It is a bad idea, unless used in assert.

Unfortunately, as of common industry practice, MFC is full of such
checks.... (Perhaps that should tell you something of quality of its
design :)

Mirek
 
R

Ram

I am going through some legacy code that has an "isNull()" method
defined on certain classes. I can see that this can be a good way to
eliminate certain types of crashes, by making this the first call in a
method (and bailing out immediately if it is true). However, if this
is such a good idea, why is it not common industry practice?

I think this is a bad idea b'coz it doesn't make a sense to call a
member function on a non-existent object.
/////////////// Sample source code //////////////
#include <iostream>

using namespace std;

class SampleClass
{
public:
SampleClass(int i):attrVal(i) {}
~SampleClass(){}

void printIt()
{
if(!isNull())
cout << "My attribute value is " << attrVal << endl;
else
cout << "I am NULL" << endl;
}

bool isNull() {return this == 0;}

protected:
int attrVal;
};



main()
{
SampleClass *ptr = NULL;

ptr->printIt();
}

A better way would be

SampleClass *ptr = NULL;
// ...

if(ptr)
ptr->printIt();
//...
 
B

benben

After all, what's the problem of making the comparison explicit?

if (!ptr_to_manager)
{
throw ...
}
else
{
ptr_to_manager->pay_my_salary();
// ...
}
 

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

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top