468,538 Members | 1,744 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,538 developers. It's quick & easy.

Warning : Possible use of null pointer

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...

Oct 5 '05 #1
26 6724
HP wrote:
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-lit....html#faq-16.6

Cheers! --M

Oct 5 '05 #2
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.

Oct 5 '05 #3
HP wrote:
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
Oct 5 '05 #4
HP wrote:
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

Oct 5 '05 #5
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

Oct 5 '05 #6
HP wrote:
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
Oct 5 '05 #7
HP wrote:

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;
}

--
Karl Heinz Buchegger
kb******@gascad.at
Oct 5 '05 #8
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?

Oct 5 '05 #9
HP wrote:
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

Oct 5 '05 #10
HP wrote:
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
Oct 5 '05 #11
HP wrote:

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.

--
Karl Heinz Buchegger
kb******@gascad.at
Oct 5 '05 #12
* 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...

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Oct 5 '05 #13
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

Oct 5 '05 #14
"HP" <hi********@rediffmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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

Oct 5 '05 #15
HP wrote:
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

Oct 5 '05 #16
Alf P. Steinbach wrote:
* Victor Bazarov:
How the hell should we know?

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...
Oct 5 '05 #17
HP
Hi all
Please tell me the optimum solution.

Oct 5 '05 #18
HP wrote:
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

Oct 5 '05 #19
HP wrote:
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
Oct 5 '05 #20
* HP:
Possible use of null pointer 'PnoSytGenericTest::_response' in
left argument to operator '->'

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");
}


Post the smallest possible complete program that exhibits this problem.

Also, does the following program,

int main()
{
int* p = new int;
*p = 42;
delete p;
}

generate any warning?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Oct 5 '05 #21
HP
Hi All
I dint find any group here reagrding MRI(Microtech research
compiler) compiler.
i am realy only on this group to find out solution.

I cant post the part of code since they are very large.

Please help me out friends

Oct 5 '05 #22
HP wrote:
Hi All
I dint find any group here reagrding MRI(Microtech research
compiler) compiler.
i am realy only on this group to find out solution.

I cant post the part of code since they are very large.

Please help me out friends


What more can we say that has not already been said?

Cheers! --M

Oct 5 '05 #23
* HP:
[forgetting to quote]

I cant post the part of code since they are very large.


Quoting myself:
"Post the smallest possible complete program that exhibits this problem."

I can think of numerous reasons why that would be difficult, e.g. you're on a
project where compilers etc. have been installed for you, and you don't know
how to compile a small program that isn't part of the project.

But all those reasons I can think of are reasons why the proper place to seek
help would be in your project organization, not here.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Oct 5 '05 #24
HP wrote:

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


Why not?
The compiler tells you all the places. You just need to fire up your
editor and insert the checkings. In the time you already wasted today
you could have fixed at least half of them.

35 files isn't much. In my current set of projects I have more then
300 files. If I make some change in the basic structures, the compiler
works for 1 and a half hour and figures out all the places where
this change takes effect. Then an editor session starts and a few hours
later everything is up to date. Not a big deal, you just need to do the
work. Welcome to programming.

--
Karl Heinz Buchegger
kb******@gascad.at
Oct 5 '05 #25
HP
Hi Karl
Thanks for moral ur boosting statements
I'll be realy thankful to you , if you know any tool that
can help me reagarding this issue.

Oct 5 '05 #26
HP wrote:
Hi Karl
Thanks for moral ur boosting statements
I'll be realy thankful to you , if you know any tool that
can help me reagarding this issue.


Tools needed: an editing program, a keyboard, your fingers, and your
mind. You'll need to adjust the code manually, methinks. The easiest
solution, since your compiler supports exceptions, is to throw your own
exception if new fails likes this:

//...
int *p = new int;
if( !p ) throw MyException();
// use p
// ...

Better would be to use std::auto_ptr, boost::scoped_ptr, or whatever to
manage each new. It gives you exception saftey with little cost, and
you don't have to remember to cleanup after yourself. Also better would
be to use std::bad_alloc and the like if you library has them.

Cheers! --M

Oct 5 '05 #27

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

16 posts views Thread by David Ford | last post: by
29 posts views Thread by junky_fellow | last post: by
27 posts views Thread by Terry | last post: by
5 posts views Thread by Martin Herbert Dietze | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.