What part of dereferencing NULL pointers causes undesired behavior?

前端 未结 16 815
长情又很酷
长情又很酷 2021-01-22 06:41

I am curious as to what part of the dereferencing a NULL ptr causes undesired behavior. Example:

//  #1
someObj * a;
a = NULL;
(*a).somefunc();   // crash, dere         


        
16条回答
  •  猫巷女王i
    2021-01-22 07:14

    Tom's comment is correct, I did not initialize correctly therefore the question is ambiguous at best yet most everyone directly answered my question, I unwittingly submitted the question while not logged in (sorry I'm new to stackoverflow) so can someone with editing powers update the OP?

    //  #2
    someObj * b;
    anotherObj * c = new anotherObj();        //initialize c
    b = NULL;
    c->anotherfunc(*b);   // *b is in question not the c dereference
    

提交回复
热议问题