The Committee felt that the functions in this section were all excellent candidates for replacement by high-performance built-in operations. Hence many simple functions have been retained, and several added, just to leave the door open for better implementations of these common operations.
The Standard reserves
function names beginning with
mem for possible future use.
have been adopted from several existing implementations.
The general goal was to provide equivalent capabilities for three types of
A block copy routine should be ``fast'': it should be implementable as a few inline instructions which take maximum advantage of any block copy provisions of the hardware. Checking for overlapping copies produces too much code for convenient inlining in many implementations. The programmer knows in a great many cases that the two blocks cannot possibly overlap, so the space and time overhead are for naught.
These arguments are contradictory but each is compelling.
Therefore the Standard mandates two block copy functions:
is required to work correctly even if the source and destination overlap,
memcpy can presume nonoverlapping operands and be
strncpy was initially introduced into the C library to
deal with fixed-length name fields in structures such as directory
Such fields are not used in the same way as strings:
the trailing null is unnecessary for a maximum-length field,
and setting trailing bytes for shorter names to null assures
efficient field-wise comparisons.
strncpy is not by origin a ``bounded
strcpy,'' and the
Committee has preferred to recognize existing practice
rather than alter the function to better suit it to such use.
Note that this function may add
n+1 characters to the string.
strxfrm provide for
locale-specific string sorting.
strcoll is intended for applications in which the number of
comparisons is small;
strxfrm is more appropriate when items are to be compared a
number of times --- the cost of transformation is then only paid once.
strstr function is an invention of the Committee.
It is included as a hook for efficient substring algorithms,
or for built-in substring instructions.
This function has been included to provide a convenient solution to many simple problems of lexical analysis, such as scanning command line arguments.
See §4.11.1, and §188.8.131.52.
This function is a descendant of
It is defined such that it can return a pointer to an in-memory read-only
string, or can copy a string into a static buffer on each call.
This function is now specified as returning a value of type