a good function should deal with unwanted parameters.
A good caller should not pass incorrect parameters. So there!
why memcpy,strcmp .etc ,these libc functions, do not
accept NULL pointer ? called with NULL, just get segment
fault. for efficiency ?
any idea ?
There's the problem of inventing some kind of "good behavior"
for functions with bogus arguments:
strcpy(NULL, "What should happen here?");
printf("How many %s can dance on a %s?\n");
longjmp(NULL, 42);
It would be a whole lot of work to define suitable behaviors for
these clearly erroneous calls, and both the compiler and library
implementors might need to expend a good deal of effort to ensure
the defined behaviors did in fact occur. (The second example might
require a very large overhaul of function linkage conventions.)
And the benefit from all this work would be ... what? To hide a
programmer's mistakes a little longer, making them harder to find
and fix? Is that a Good Thing?
There are a few cases where "suitable behavior" might not be
too hard to define. memcpy(NULL, NULL, 0) could be defined as a
no-op, for example, and that might be a "reasonable" outcome. But
c'mon: It's quite reasonable that the count could be zero in a
limiting case, but is it reasonable for a program to supply either
of the pointer arguments as NULL? Insisting on a well-defined
outcome for such an implausible scenario seems of little value,
especially if it threatens to penalize all the more sensible uses.
Finally, there are a few cases where the library does in fact
accept NULL arguments and define their meaning. free(NULL) is
well-defined, as is realloc(NULL, 42); NULL values arise "naturally"
in some limiting cases. The library also uses NULL's as special
values in setbuf(), setvbuf(), and fflush(), and there may be a few
more NULLable parameters I've overlooked. But by and large the
philosophy is "trust the programmer" (Rationale, section 0). Part
of that "trust" is "trust the programmer not to do silly things;"
the question of whether a language *should* be so trusting is fodder
for an entirely different debate.