Is my check for NULL pointer incorrect

S

Slain

By mistake I passed a pointer not initialized to a method. This method
explicitly checks for NULL pointer and returns if that is the case.

It seems that I still bypassed this check. Here is what I am trying to
do

func1(.., A *aa)
{
if (aa == NULL)
return 0;
:
:
}

When I print aa using gdb in that section, I get the output as
(A*) 0x0

I assume that I should have just returned when func1 saw aa to be
NULL, but I crashed later on in func1()

Any ideas?

Thanks
 
V

Victor Bazarov

Slain said:
By mistake I passed a pointer not initialized to a method. This method
explicitly checks for NULL pointer and returns if that is the case.

It seems that I still bypassed this check. Here is what I am trying to
do

func1(.., A *aa)
{
if (aa == NULL)
return 0;
:
:
}

When I print aa using gdb in that section, I get the output as
(A*) 0x0

I assume that I should have just returned when func1 saw aa to be
NULL, but I crashed later on in func1()

Any ideas?

Yes, I think your problem is covered in the FAQ 5.8.

V
 
S

Slain

Okay here is a better attempt. I was trying to keep it easy to read


namespace Utility {

RESULT
validateUtility(const char *FileName, ID Type, const char *pCode,
LoadFile* load)
{

if(load == NULL)
return 0;

//get the file
load.getDATA();

}

I crash in load.getDATA();
cause when I look at load it is
<LoadFile*> 0x0
 
V

Victor Bazarov

Slain said:
Okay here is a better attempt. I was trying to keep it easy to read


namespace Utility {

RESULT
validateUtility(const char *FileName, ID Type, const char *pCode,
LoadFile* load)
{

if(load == NULL)
return 0;

//get the file
load.getDATA();

}

I crash in load.getDATA();
cause when I look at load it is
<LoadFile*> 0x0

Say, whom are you trying to kid? Post real code please. Otherwise,
it's GIGO. If you aren't being honest with us, what do you want in return?

V
 
S

Slain

It has less to do with honesty and more with pasting huge chunks of
code? It involves the interaction of two components and mcuh more work
to explain that part. Hence I wrote a very similar prototype. Is the
real aim to help clear doubts or to debug code.

The question is straightforward. When my pointer looks like it has
nothing in it, why is the NULL check failing? That is it. I don't
really see how pasting real code would help any better.
 
A

Alf P. Steinbach

* Slain:
It has less to do with honesty and more with pasting huge chunks of
code? It involves the interaction of two components and mcuh more work
to explain that part. Hence I wrote a very similar prototype. Is the
real aim to help clear doubts or to debug code.

The question is straightforward. When my pointer looks like it has
nothing in it, why is the NULL check failing? That is it. I don't
really see how pasting real code would help any better.

In addition to stop posting fake code, and stop arguing the good advice you get,
could you please refrain from top-posting in this group?

And please also trim your quoting, e.g., don't quote signatures.

And perhaps, please, read the FAQ before posting, as you've been adviced to do.



Cheers,

- Alf
 
S

Slain

* Slain:



In addition to stop posting fake code, and stop arguing the good advice you get,
could you please refrain from top-posting in this group?

And please also trim your quoting, e.g., don't quote signatures.

And perhaps, please, read the FAQ before posting, as you've been adviced to do.




Cheers,

- Alf


Well, sadly this has become an issue more of honesty than the real
purpose of such a group.

I am not looking for solutions to fix my code, cause like I said I
passed in a bad value by mistake. But more for reasoning as to why
sucha thing might happen.

I cannot paste corporate copyrighted code and hence tried my best to
create a very similar looking prototype. Sadly, it seems there is a
higher need to follow process than to help.
 
K

Krice

why is the NULL check failing?

Don't check NULL there. Make sure that when you call the
function all objects that you pass exist. That way you
always give real objects to the function and don't have
to check for NULL.
 
P

Paul N

The question is straightforward. When my pointer looks like it has
nothing in it, why is the NULL check failing? That is it. I don't
really see how pasting real code would help any better.

When the code fails, are you actually passing in NULL or are you
passing in an uninitialised value? The fact that you haven't set a
variable to any particular value is no guarantee (at least for
automatic variables) that the variable has the value NULL. You could
have some random value which is not NULL (and so does not fire your
test) but isn't a pointer to a valid object either.

Try to draw up some short but valid code that is similar to your
problem code. Does it have the bug or not? If it does, please post it;
if not, try to work out why your real code is different from the
tested code.

Hope that helps.
Paul.
 
J

Juha Nieminen

Slain said:
By mistake I passed a pointer not initialized to a method. This method
explicitly checks for NULL pointer and returns if that is the case.

Your error is that you assume that uninitialized pointers will be
initialized to NULL.

Read that again, and you might see the contradiction.
 
J

Jonathan Lee

The question is straightforward. When my pointer looks like it has
nothing in it, why is the NULL check failing?

Because your pointer does not equal NULL. This could only occur
for two reasons:
1) the value is not NULL
2) the NULL macro is defined incorrectly

Changing the test to this:

if (yourptr != NULL) {
cout << "*** My pointer is " << yourptr << endl;
cout << "*** NULL is " << (NULL) << endl;
} else return 0;

should tell you what's going on. It has already been suggested that
you may be passing an _uninitialized_ pointer, which will not
necessarily be a null pointer. An error like this may be hidden
by the debugger.
That is it. I don't really see how pasting real code would help
any better.

Because there is no good reason for a null pointer to not be
equal to NULL. So this is probably not the source of your bug,
like you've assumed. We cannot divine what the true problem is,
however, without seeing actual code that reproduces it.

And no, code that *does not even compile* cannot be said to
reproduce the problem.

--Jonathan
 
S

SeanW

I cannot paste corporate copyrighted code and hence tried my best to
create a very similar looking prototype. Sadly, it seems there is a
higher need to follow process than to help.

Please don't take offense at the gruff responses.
We *want* to help you, but it's impossible if
you don't post code that actually reproduces
the problem. It's a question of reality, not
protocol.

I know it's a lot of work to isolate a problem
down to a small example, but it's just not
feasible to say to us "I can't show you my code,
but please tell me what's wrong with it".

Plus, isolating the problem usually results in you
finding the problem yourself; bonus!

Sean
 
D

Default User

Slain wrote:

I cannot paste corporate copyrighted code and hence tried my best to
create a very similar looking prototype. Sadly, it seems there is a
higher need to follow process than to help.


No, but you can reduce the code to create a minimal example that
demonstrates the problem. Often by doing so, you will discover the
error on your own.




Brian
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top