Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Warning : Possible use of null pointer

Reply
Thread Tools

Warning : Possible use of null pointer

 
 
HP
Guest
Posts: n/a
 
      10-05-2005
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...

 
Reply With Quote
 
 
 
 
=?iso-8859-1?Q?Ali_=C7ehreli?=
Guest
Posts: n/a
 
      10-05-2005
"HP" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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

 
Reply With Quote
 
 
 
 
mlimber
Guest
Posts: n/a
 
      10-05-2005
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

 
Reply With Quote
 
HP
Guest
Posts: n/a
 
      10-05-2005
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.

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      10-05-2005
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
 
Reply With Quote
 
mlimber
Guest
Posts: n/a
 
      10-05-2005
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

 
Reply With Quote
 
HP
Guest
Posts: n/a
 
      10-05-2005
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

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      10-05-2005
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
 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      10-05-2005
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
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
HP
Guest
Posts: n/a
 
      10-05-2005
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?

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
possible NULL && dereferencing NULL pointer Mark C Programming 8 02-01-2012 03:15 PM
pointer to pointer intialize to NULL but still point to NULL Christopher C++ 4 07-09-2011 12:35 AM
Null pointer (NULL array pointer is passed) aneuryzma C++ 3 06-16-2008 05:48 AM
"stringObj == null" vs "stringObj.equals(null)", for null check?? qazmlp1209@rediffmail.com Java 5 03-29-2006 10:37 PM
NULL macro vs. 0 as null pointer? Ken C++ 69 07-31-2004 04:45 AM



Advertisments