In either C or C++, should I check pointer parameters against NULL/nullptr?

后端 未结 20 2312
有刺的猬
有刺的猬 2020-11-27 03:54

This question was inspired by this answer.

I\'ve always been of the philosophy that the callee is never responsible when the caller does something stupid, like passi

20条回答
  •  情话喂你
    2020-11-27 04:44

    For C++, if your function doesn't accept nullpointer, then use a reference argument. In general.

    There are some exceptions. For example, many people, including myself, think it's better with pointer argument when the actual argument will most naturally be a pointer, especially when the function stores away of a copy of the pointer. Even when the function doesn't support nullpointer argument.

    How much to defend against invalid argument depends, including that it depends on subjective opinion and gut-feeling.

    Cheers & hth.,

提交回复
热议问题