Warning : Possible use of null pointer

Discussion in 'C++' started by HP, Oct 5, 2005.

  1. HP

    HP Guest

    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...
     
    HP, Oct 5, 2005
    #1
    1. Advertising

  2. "HP" <> wrote in message
    news:...

    > 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
     
    =?iso-8859-1?Q?Ali_=C7ehreli?=, Oct 5, 2005
    #2
    1. Advertising

  3. HP

    mlimber Guest

    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-lite/freestore-mgmt.html#faq-16.6

    Cheers! --M
     
    mlimber, Oct 5, 2005
    #3
  4. HP

    HP Guest

    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.
     
    HP, Oct 5, 2005
    #4
  5. 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
     
    Victor Bazarov, Oct 5, 2005
    #5
  6. HP

    mlimber Guest

    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
     
    mlimber, Oct 5, 2005
    #6
  7. HP

    HP Guest

    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
     
    HP, Oct 5, 2005
    #7
  8. 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
     
    Victor Bazarov, Oct 5, 2005
    #8
  9. 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
     
    Karl Heinz Buchegger, Oct 5, 2005
    #9
  10. HP

    HP Guest

    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?
     
    HP, Oct 5, 2005
    #10
  11. HP

    mlimber Guest

    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
     
    mlimber, Oct 5, 2005
    #11
  12. 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
     
    Victor Bazarov, Oct 5, 2005
    #12
  13. 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
     
    Karl Heinz Buchegger, Oct 5, 2005
    #13
  14. * 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?
     
    Alf P. Steinbach, Oct 5, 2005
    #14
  15. HP

    HP Guest

    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
     
    HP, Oct 5, 2005
    #15
  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...
     
    Victor Bazarov, Oct 5, 2005
    #16
  17. HP

    mlimber Guest

    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
     
    mlimber, Oct 5, 2005
    #17
  18. HP

    HP Guest

    Hi all
    Please tell me the optimum solution.
     
    HP, Oct 5, 2005
    #18
  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
     
    Victor Bazarov, Oct 5, 2005
    #19
  20. HP

    mlimber Guest

    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
     
    mlimber, Oct 5, 2005
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    27,496
    Mike Schilling
    Mar 29, 2006
  2. aneuryzma
    Replies:
    3
    Views:
    780
    Jim Langston
    Jun 16, 2008
  3. Christopher
    Replies:
    4
    Views:
    465
    Ruben Safir
    Jul 9, 2011
  4. Mark
    Replies:
    8
    Views:
    685
    Joe keane
    Feb 1, 2012
  5. Julian Mehnle
    Replies:
    17
    Views:
    951
    Julian Mehnle
    May 18, 2006
Loading...

Share This Page