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

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

    Overhead of development time + runtime performance has a trade-off with the robustness of the API you are designing.

    If the API you are publishing has to run inside the process of the calling routine, you SHOULD NOT check for NULL or invalid arguments. In this scenario, if you crash, the client program crashes and the developer using your API should mend his ways.

    However, if you are providing a runtime/ framework which will run the client program inside it (e.g., you are writing a virtual machine or a middleware which can host the code or an operating system), you should definitely check of the correctness of the arguments passed. You don't want your program to be blamed for the mistakes of a plugin.

提交回复
热议问题