Warning : Possible use of null pointer

H

HP

Hi guys
i wana remove this warning which i am getting during my
codcomilation.

the warning is -
Possible use of null pointer 'PnoSytGenericTest::_response' in
left argument to operator '->'



and the part of the code related to this warning-



void PnoSytGenericTest::initResponse()
{
ENTER_METHOD("SYT_TEST_MGR", PnoSytGenericTest::initResponse);

_response = new AcdMessage();
_response->header(_data->header());
(_response->header()).cmdId[0].cmdId = Acd::CMD_RSP;

RETURN_METHOD("SYT_TEST_MGR");
}



help me out friends...
 
?

=?iso-8859-1?Q?Ali_=C7ehreli?=

the warning is -
Possible use of null pointer 'PnoSytGenericTest::_response' in
left argument to operator '->'



and the part of the code related to this warning-



void PnoSytGenericTest::initResponse()
{
ENTER_METHOD("SYT_TEST_MGR", PnoSytGenericTest::initResponse);

_response = new AcdMessage();
_response->header(_data->header());
(_response->header()).cmdId[0].cmdId = Acd::CMD_RSP;

RETURN_METHOD("SYT_TEST_MGR");
}

I can think of three possibilities:

1) You have a brain-dead or very old compiler, which thinks that new may
return 0. (new does not return 0, it throws std::bad_alloc.)

2) new is evilly #defined as new(nothrow). In that case new does not throw
but returns 0.

3) _response is used within the ENTER_METHOD macro, but of course we don't
see that in the code given

Ali
 
M

mlimber

HP said:
Hi guys
i wana remove this warning which i am getting during my
codcomilation.

the warning is -
Possible use of null pointer 'PnoSytGenericTest::_response' in
left argument to operator '->'



and the part of the code related to this warning-



void PnoSytGenericTest::initResponse()
{
ENTER_METHOD("SYT_TEST_MGR", PnoSytGenericTest::initResponse);

_response = new AcdMessage();
_response->header(_data->header());
(_response->header()).cmdId[0].cmdId = Acd::CMD_RSP;

RETURN_METHOD("SYT_TEST_MGR");
}



help me out friends...

What compiler are you using and do you have exceptions enabled? In
standard C++, _response could not be null because new throws an
exception on failure. On non-conformant implementations where this
isn't true or if you have disabled exceptions, then you should check
for null. See this FAQ:

http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.6

Cheers! --M
 
H

HP

hey Mlimber
i am using MRI (Microtech research INC.)
compiler.
and i have exception enabled.
but i dont think on failure of new i am getting this warrning.
 
V

Victor Bazarov

HP said:
i wana remove this warning which i am getting during my
codcomilation.

What's "codcomilation"? Is that some kind of rite you perform? Or
is that an exam (test) you're taking at school? My dictionary does
not contain that word... I know 'cod' is a kind of fish... Are you
talking about performing something on a cod? Gutting? Cleaning?
Is it dead or alive? Probably doesn't matter...
the warning is -
Possible use of null pointer 'PnoSytGenericTest::_response' in
left argument to operator '->'



and the part of the code related to this warning-



void PnoSytGenericTest::initResponse()
{
ENTER_METHOD("SYT_TEST_MGR", PnoSytGenericTest::initResponse);

_response = new AcdMessage();
_response->header(_data->header());
(_response->header()).cmdId[0].cmdId = Acd::CMD_RSP;

RETURN_METHOD("SYT_TEST_MGR");
}

What *line* is the warning for? It is possible that you're using such
an old compiler that can return a null pointer as a result of 'new'.
If that's so, upgrade your compiler. Apparently it thinks that the
'_response' variable can become a null pointer, which is non-standard
behaviour. You could work around this by doing

_response = new AcdMessage();
if (_response) {
...
}
RETURN_METHOD...
help me out friends...

Not much to go on, just to let you know.

V
 
M

mlimber

HP said:
hey Mlimber
i am using MRI (Microtech research INC.)
compiler.
and i have exception enabled.
but i dont think on failure of new i am getting this warrning.

I don't know about Microtech Research. You'd better check your
documentation or the standard library source code to find out how
conformant your compiler is. It sounds to me like it is not conformant
on this point.

If the compiler isn't conformant, you can likely get rid of the warning
by checking for null:

void PnoSytGenericTest::initResponse()
{
ENTER_METHOD("SYT_TEST_MGR", PnoSytGenericTest::initResponse);
_response = new AcdMessage();
if( 0 == _response ) throw MyException();
_response->header(_data->header());
_response->header().cmdId[0].cmdId = Acd::CMD_RSP;
RETURN_METHOD("SYT_TEST_MGR");
}

Cheers! --M
 
H

HP

Hi Victor
sorry for spelling mistake ( that was code compilation)
I am not using old compiler. I think the reason which you have put is
right only when someone using the older version of compiler.
but here the case is not the same.
I think there is some other reson.

awaiting for your reply
 
V

Victor Bazarov

HP said:
hey Mlimber
i am using MRI (Microtech research INC.)
compiler.
and i have exception enabled.
but i dont think on failure of new i am getting this warrning.

Split all the lines where -> is used and tell us what *line* the
error is reported for.

V
 
K

Karl Heinz Buchegger

HP said:
Hi guys
i wana remove this warning which i am getting during my
codcomilation.

the warning is -
Possible use of null pointer 'PnoSytGenericTest::_response' in
left argument to operator '->'

and the part of the code related to this warning-

void PnoSytGenericTest::initResponse()
{
ENTER_METHOD("SYT_TEST_MGR", PnoSytGenericTest::initResponse);

_response = new AcdMessage();
_response->header(_data->header());
(_response->header()).cmdId[0].cmdId = Acd::CMD_RSP;

RETURN_METHOD("SYT_TEST_MGR");
}

help me out friends...

It seems like your compiler is an older one. In Standard C++ it
is impossible for new to return NULL. new throws an exception in
case of a problem such as 'out of memory'.

As said: your compiler knows that new may return NULL and it is warning
against that possibility.
Q: So what will you do against it?
A: Check that the returned pointer is not NULL:

_response = new AcdMessage();
if( _response ) {
_response->header(_data->header());
(_response->header()).cmdId[0].cmdId = Acd::CMD_RSP;
}
 
H

HP

Hi All
i have commented those two line which is my compiler showing
as warining


void PnoSytGenericTest::initResponse()
{
ENTER_METHOD("SYT_TEST_MGR", PnoSytGenericTest::initResponse);

_response = new AcdMessage();
_response->header(_data->header()); // THIS IS THE LINE OF WARNING
(_response->header()).cmdId[0].cmdId = Acd::CMD_RSP; // THIS IS THE
LINE OF WARNING

RETURN_METHOD("SYT_TEST_MGR");
}




one thing else even after use of delete i am getting the same kind of
warning
is it compiler check for NULLl even after use of delete?
 
M

mlimber

HP said:
Hi Victor
sorry for spelling mistake ( that was code compilation)
I am not using old compiler. I think the reason which you have put is
right only when someone using the older version of compiler.
but here the case is not the same.
I think there is some other reson.

awaiting for your reply

The compiler may not be old, but it does appear to be non-conformant.
Check your documentation or contact your vendor to find out. If it is
non-conformant, follow our uniform advice on this thread.

Cheers! --M
 
V

Victor Bazarov

HP said:
i have commented those two line which is my compiler showing
as warining


void PnoSytGenericTest::initResponse()
{
ENTER_METHOD("SYT_TEST_MGR", PnoSytGenericTest::initResponse);

_response = new AcdMessage();
_response->header(_data->header()); // THIS IS THE LINE OF WARNING
(_response->header()).cmdId[0].cmdId = Acd::CMD_RSP; // THIS IS THE
LINE OF WARNING

RETURN_METHOD("SYT_TEST_MGR");
}

Did you see other responses? If you're sure that the compiler is not
too old to be non-standard WRT what 'new' returns, just disable that
warning (consult the compiler documentation to find out how to do that)
or add checking of '_response' for being null before using it.
one thing else even after use of delete i am getting the same kind of
warning

I don't see any 'delete' in the code above.
is it compiler check for NULLl even after use of delete?

How the hell should we know? Contact the compiler technical support.
This newsgroup cannot help you with any particular compiler.

V
 
K

Karl Heinz Buchegger

HP said:
one thing else even after use of delete i am getting the same kind of
warning
is it compiler check for NULLl even after use of delete?

Show the code in question.
But: you are not allowed to dereference a pointer, once you used it for delete.
Seems like your compiler does a good job in this case.
 
A

Alf P. Steinbach

* Victor Bazarov:
How the hell should we know?

Uhm, I think that perhaps you're a bit edgy right now?

Sometimes seemingly obtuse questions are due to what one may call a semantic
gap, that (most often) the person lacks some critical small piece of knowledge
without which our perfectly clear statements simply do not make sense.

For example, when I was a student, one lecturer had a habit of using
mathematical notation for just about everything. Writing things such as
"f:t->u". Now that was incomprehensible to me because I didn't know the
notation, and because the _concepts_ he tried to get across were so utterly
basic that I couldn't believe that was what he tried to say, so I parsed his
notation as "(f:t)->u" in order to find the presumed hidden meaning...
 
H

HP

Hi all
the part of the code is which is generating warning due
to use of delete is

AcdCmdFailureBody *body_v = new AcdCmdFailureBody;
if( body_v )
{
if( _response->body() )
delete _response->body(); // GENERATING WARNING
_response->body( body_v );


Tell me the possible solution since :-

I cant do the option of turning off the warning by setting the
compiler.
and 14 to 15 warning is comming in one file and i have near about 35
files.
so i cant put those null checking condition everywhere
so tell me some posible as well as optimum solution for that problem
 
V

Victor Bazarov

Alf said:
* Victor Bazarov:



Uhm, I think that perhaps you're a bit edgy right now?

Why would you say that? The question was about the compiler. It's OT,
it's not your mainstream compiler either. How the hell should we know
if it's checking for NULL after delete?
Sometimes seemingly obtuse questions are due to what one may call a semantic
gap, that (most often) the person lacks some critical small piece of knowledge
without which our perfectly clear statements simply do not make sense.

Semantic gap my foot! Does my compiler check for NULL after 'delete'?
How can you answer that?
For example, when I was a student, [...]

Good memory! I don't remember much from when I was a student...
 
M

mlimber

HP said:
Hi all
the part of the code is which is generating warning due
to use of delete is

AcdCmdFailureBody *body_v = new AcdCmdFailureBody;
if( body_v )
{
if( _response->body() )
delete _response->body(); // GENERATING WARNING
_response->body( body_v );


Tell me the possible solution since :-

I cant do the option of turning off the warning by setting the
compiler.
and 14 to 15 warning is comming in one file and i have near about 35
files.
so i cant put those null checking condition everywhere
so tell me some posible as well as optimum solution for that problem

First of all, I notice that the original programmer (presumably not
you) checked for null after his/her new here. If your compiler is
non-conformant and you want safe code, you *must* do likewise. If you
choose not to, then you can live with the warnings (which are not
errors, please note) or disable them with a compiler switch or #pragma
or whatever.

As for the delete, I'm guessing that Response::body() returns a member
pointer and Response::Body( AcdCmdFailureBody* ) assigns that same
pointer. If so, you can safely delete the "if" statement since it's ok
to delete null:

AcdCmdFailureBody *body_v = new AcdCmdFailureBody;
if( body_v )
{
delete _response->body(); // GENERATING WARNING
_response->body( body_v );
// ...
}

(Of course, if Response::body() returns the "this" pointer, then you're
in for a mess of trouble. But we don't know what it does, since you
only post non-compilable sections of code.)

Cheers! --M
 
V

Victor Bazarov

HP said:
the part of the code is which is generating warning due
to use of delete is

AcdCmdFailureBody *body_v = new AcdCmdFailureBody;

So, you allocate new 'body_v'. OK.
if( body_v )

And you check if the allocation went through. OK. It shows, however,
that your compiler is non-compliant, if it *requires* you to check.
{
if( _response->body() )
delete _response->body(); // GENERATING WARNING

Could it be that '_response' is a null pointer here? Then again, the
warning ought to be for one line before this.
_response->body( body_v );

And here, do you get a warning? If not, it may not be '_response', but
instead something in the 'body' function.
Tell me the possible solution since :-

I cant do the option of turning off the warning by setting the
compiler.

Why not?
and 14 to 15 warning is comming in one file and i have near about 35
files.

So? I have 150 files, and I am not crying about it. It's your project,
deal with it.
so i cant put those null checking condition everywhere
so tell me some posible as well as optimum solution for that problem

I'll tell you: call your compiler technical support. They should NOT
be warning your about NULL _unless_ they really *can* return NULL from
'new'. In that case, they are non-standard, and the only way for you
to write code without undefined behaviour is to put "those null checking
condition everywhere". Period.

V
 
M

mlimber

HP said:
Hi all
Please tell me the optimum solution.

We have already given you the optimal advice that we can with the
limited information you have supplied. You need to consult your
documentation, talk to your compiler vendor, or post to a newsgroup
related to your compiler. This newsgroup is for standard C++ language
issues, not compiler-specific problems.

Alternately, I'm sure one of us would be willing to solve your problems
for you for some enormously exorbitant consulting fees.

Cheers! --M
 

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,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top