The whole C language is written with the motto "We'll behave correctly provided the programmer knows what he's doing." The programmer is expected to know to make all the checks he needs to make. It's not just checking for NULL, it's ensuring that dest
points to enough allocated memory to hold src
, it's checking the return value of fopen
to make sure the file really did open successfully, knowing when memcpy
is safe and when memmove
is required, and so on.
Getting strcpy
to check for NULL won't change the language paradigm. You will still need to ensure that dest
points to enough space -- and this is something that strcpy
can't check for without changing the interface. You will also need to ensure that src
is '\0'
-terminated, which again strcpy
can't possibly check.
There are some C standard library functions which do check for NULL: for example, free(NULL)
is always safe. But in general, C expects you to know what you're doing.
[C++ generally eschews the <cstring>
library in favour of std::string
and friends.]
NULL
checks onstrcpy
because all buffers are statically allocated and used directly. There is absolutely no way I will ever passNULL
tostrcpy
. So why would I want to pay the price? There's no "do not think I need" about it. – MetaphorNULL
checks in functions that do not assign special meaning toNULL
arguments are a bane of bad C libraries. They lock you into added waste and encourage bad coders to tossNULL
pointers around as if they were a universally-valid "empty string" or something. – Intercessor