用C或C ++,我应该检查是否为NULL / nullptr指针参数? [英] In either C or C++, should I check pointer parameters against NULL/nullptr?
问题描述
这问题是由<一个启发href=\"http://programmers.stackexchange.com/questions/24567/how-do-i-assess-whether-my-expectations-to-the-interviewees-are-adequate/24595#24595\">this回答。
我一直的理念是,被叫方从未负责当呼叫者做一些愚蠢的事,像传递无效参数。我得出这一结论已经抵达了几个原因,但也许最重要的是来自这篇文章:
I've always been of the philosophy that the callee is never responsible when the caller does something stupid, like passing of invalid parameters. I have arrived at this conclusion for several reasons, but perhaps the most important one comes from this article:
没有定义一切是不确定的。
Everything not defined is undefined.
如果一个函数没有在它的文档说,这是有效的传递 nullptr
,那么你绝对做得更好未通过 nullptr
该功能。我不认为这是被调用的函数来处理这样的事情的责任。
If a function doesn't say in it's docs that it's valid to pass nullptr
, then you damn well better not be passing nullptr
to that function. I don't think it's the responsibility of the callee to deal with such things.
不过,我知道有一些将要谁与我不同意。我很好奇,我是否应该将检查这些东西,以及为什么。
However, I know there are going to be some who disagree with me. I'm curious whether or not I should be checking for these things, and why.
推荐答案
虽然总的来说我没有看到在一个公共API检测NULL(NULL的原因,而不是其他一些无效的地址?)值我可能还是这样做只是因为许多C和C ++程序员希望这样的行为。
While in general I don't see the value in detecting NULL (why NULL and not some other invalid address?) for a public API I'd probably still do it simply because many C and C++ programmers expect such behavior.
这篇关于用C或C ++,我应该检查是否为NULL / nullptr指针参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!