What part of dereferencing NULL pointers causes undesired behavior?

前端 未结 16 1017
长情又很酷
长情又很酷 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条回答
  •  梦谈多话
    2021-01-22 06:51

    Dereferencing a NULL pointer is undefined behavior.

    It is not guaranteed to crash, and you are not guaranteed anything when doing it. For all you know someone somewhere in the world will be punched each time you do it. That is valid behavior since it is undefined.

    Also your pointers may not be initialized to NULL so if you want them to be for sure NULL you should set them explicitly to NULL.

提交回复
热议问题