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

后端 未结 20 2082
有刺的猬
有刺的猬 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:52

    In my view, it's not a question of responsibility. It's a question of robustness.

    Unless I have full control on the caller and I must optimize for even the minute speed improvement, I always check for NULL.

    0 讨论(0)
  • 2020-11-27 04:53

    I lean heavily on the side of 'don't trust your user's input to not blow up your system' and in defensive programming in general. Since I have made APIs in a past life, I have seen users of the libraries pass in null pointers and then application crashes result.

    If it is truly an internal library and I'm the only person (or only a select few) have the ability to use it, then I might ease up on null pointer checks as long as everyone agrees to abide by general contracts. I can't trust the user base at large to adhere to that.

    0 讨论(0)
提交回复
热议问题