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

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

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

    Defense in Depth principle says yes. If this is an external API then totally essential. Otherwise, at least an assert to assist in debugging misuse of your API.

    You can document the contract until you are blue in the face, but you cannot in callee code prevent ill-advised or malicious misuse of your function. The decision you have to make is what's the likely cost of misuse.

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

    If you don't want a NULL then don't make the parameter a pointer.
    By using a reference you guarantee that the object will not be NULL.

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

    If you're going to check for NULL pointer arguments where you have not entered into a contract to accept and interpret them, do it with an assert, not a conditional error return. This way the bugs in the caller will be immediately detected and can be fixed, and it makes it easy to disable the overhead in production builds. I question the value of the assert except as documentation however; a segfault from dereferencing the NULL pointer is just as effective for debugging.

    If you return an error code to a caller which has already proven itself buggy, the most likely result is that the caller will ignore the error, and bad things will happen much later down the line when the original cause of the error has become difficult or impossible to track down. Why is it reasonable to assume the caller will ignore the error you return? Because the caller already ignored the error return of malloc or fopen or some other library-specific allocation function which returned NULL to indicate an error!

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

    My philosophy is: Your users should be allowed to make mistakes, your programming team should not.

    What this means is that the only place you should check for invalid parameters including NULL, is in the top-level user interface. Everywhere the user can provide input to your code, you should check for errors, and handle them as gracefully as possible.

    Everywhere else, you should use ASSERTS to ensure the programmers are using the functions correctly.

    If you are writing an API, then only the top-level functions should catch and handle bad input. It is pointless to keep checking for a NULL pointer three or four levels deep into your call stack.

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

    In my opinion, it's the callee's responsibility to enforce its contract.

    If the callee shouldn't accept NULL, then it should assert that.

    Otherwise, the callee should be well behaved when it's handed a NULL. That is, either it should functionally be a no-op, return an error code, or allocate its own memory, depending on the contract that you specified for it. It should do whatever seems to be the most sensible from the caller's perspective.

    As the user of the API, I want to be able to continue using it without having the program crash; I want to be able to recover at the least or shut down gracefully at worst.

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