What part of dereferencing NULL pointers causes undesired behavior?

前端 未结 16 812
长情又很酷
长情又很酷 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 07:03

    Reading from or writing to the invalid memory location causes a crash.

    A call to a member function through an invalid object pointer will usually succeed, if the method is not virtual and the method does not access any members of the object, since this involves no reads or writes related to the object pointer.

    (This is not guaranteed by the standard, even though it work that way on all compilers i ever encountered)

提交回复
热议问题