What part of dereferencing NULL pointers causes undesired behavior?

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

    It would still cause a crash, but that's not necessarily undesired behaviour. Part of the usefulness of NULL is that, on most platforms, it points to memory that is explicitly inaccessible to your application, and causes a segmentation fault (or access violation) the very moment you try to dereference it.

    Its purpose is to explicitly mark the contents of pointers as invalid.

提交回复
热议问题