By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,422 Members | 1,615 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,422 IT Pros & Developers. It's quick & easy.

isNull() method a good idea?

P: n/a
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();
}

Aug 1 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a
Generic Usenet Account wrote:
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

Aug 1 '05 #2

P: n/a
Generic Usenet Account wrote:
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
Aug 1 '05 #3

P: n/a
Ian
Generic Usenet Account wrote:
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
Aug 1 '05 #4

P: n/a
Ian wrote:

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

Aug 1 '05 #5

P: n/a
Generic Usenet Account wrote:
Ian wrote:
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


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
Aug 1 '05 #6

P: n/a
Larry I Smith wrote:
Generic Usenet Account wrote:
Ian wrote:

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

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
Aug 1 '05 #7

P: n/a
Ian
Generic Usenet Account wrote:
Ian wrote:

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)!

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
Aug 1 '05 #8

P: n/a
Larry I Smith wrote:
Generic Usenet Account wrote:
Ian wrote:
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


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
Aug 1 '05 #9

P: n/a
Victor Bazarov wrote:
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)
Aug 2 '05 #10

P: n/a
> 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
Aug 2 '05 #11

P: n/a
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();
//...

Aug 2 '05 #12

P: n/a
After all, what's the problem of making the comparison explicit?

if (!ptr_to_manager)
{
throw ...
}
else
{
ptr_to_manager->pay_my_salary();
// ...
}
Aug 3 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.