473,388 Members | 1,309 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

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 7657
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
by: MK | last post by:
I am a newbie. Please help. The following warning is issued by gcc-3.2.2 compiler (pc Linux): ================================================================== read_raw_data.c:51: warning:...
16
by: David Ford | last post by:
I have a macro that I use across the board for freeing ram. I'd like to clean up my code so I don't get these warnings. #define sfree(x) _internal_sfree((void **)&x) #define _internal_sfree(x)...
29
by: junky_fellow | last post by:
Consider the following piece of code: struct junk { int i_val; int i_val1; char c_val; }; int main(void) {
3
by: Bas Wassink | last post by:
Hello there, I'm having trouble understanding a warning produced by 'splint', a code-checker. The warning produced is: keywords.c: (in function keyw_get_string) keywords.c:60:31: Released...
4
by: Varun Kacholia | last post by:
Hi everyone, I have the following piece of code: vector<MyClass*> my_vector(10, NULL); However this throws the following errors: myfile.cc: warning: converting NULL to non-pointer type...
27
by: Terry | last post by:
I am getting the following warning for the below function. I understand what it means but how do I handle a null reference? Then how do I pass the resulting value? Regards Warning 1...
4
by: metalinc | last post by:
hi...im new to C programming...need help...tried to run this code but got this error fork.c: In function ‘parse’: fork.c:44: warning: comparison between pointer and integer fork.c:51: warning:...
5
by: Martin Herbert Dietze | last post by:
Hello, consider this code: | /* warning613.c */ | #include <assert.h> | #include <string.h> | | size_t | myStrLen(char const* s)
3
by: mrmattborja | last post by:
Hello, Here is a program I'm playing around with for fun in the process of learning C. The objective is to create a function filesize() and call it from within the main() section to retrieve the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.