Chapter Contents |
Previous |
Next |
Function Categories |
a
str
mem
mem
routines are always passed an explicit string length since the
string may contain no null characters, or more than one null character.
Two standard string functions that begin with the letters
str
,
strcoll
,
and
strxfrm
pertain to localization and
are discussed in Chapter 10, "Localization," in
SAS/C Library Reference, Volume 2.
The following are string functions:
Terms Used in String Function Descriptions |
These terms are used in the descriptions of string utility functions:
int
or
unsigned
position of a character in a string compute the position beginning
at 0.
Optimizing Your Use of memcmp, memcpy, and memset |
You can optimize your use of the built-in functions
memcmp
,
memcpy
, and
memset
by controlling the type of the
length
argument. The compiler inspects the type before the argument
is converted to the type specified in the function prototype. If the type
of the
length
argument is one of the types
in Types Acceptable as Length Arguments in Built-in Functions,
the compiler generates only the code required for the maximum value of the
type. Types Acceptable as Length Arguments in Built-in Functions shows the maximum values of these types. Note that these values can be obtained
from the
<limits.h>
header file.
You can use only the types shown in Types Acceptable as Length Arguments in Built-in Functions (in addition to
size_t
). If the
length
argument
has any other type, the compiler issues a warning message.
Type | Maximum Value |
---|---|
char
|
255 |
unsigned char
|
255 |
short
|
32767 |
signed short
|
32767 |
unsigned short
|
65535 |
If
Types Acceptable as Length Arguments in Built-in Functions lists the type of the
length
argument,
the function will not be required to operate on more than 16 megabytes of
data. Therefore, the compiler does not generate a call to the true (that
is, separately linked) function to handle that case.
If the
length
argument
is one of the
char
types, the compiler
generates a MOVE instruction (which can handle up to 256 characters) rather
than a MOVE LONG (which can handle up to 16 megabytes of characters). Because
the MOVE LONG instruction is one of the slowest instructions in the IBM 370
instruction set, generating a MOVE saves execution time.
Getting the Most Efficient Code |
<string.h>
or
<lcstring.h>
, and do not use the function name in an
#undef
preprocessing directive.
length
argument as one of the types in Types Acceptable as Length Arguments in Built-in Functions.
length
argument to a wider type.
This defeats the compiler's inspection
of the type.
You may want to define one or more macros that cast the
length
argument to a shorter type. For example,
here is a program that defines two such macros:
#include <string.h> /* Copy up to 32767 characters. */ #define memcpys(to, from, length) memcpy(to, from, (short)length) /* Copy up to 255 characters. */ #define memcpyc(to, from, length) memcpy(to, from, (char)length) . . . int strsz; /* strsz is known to be less than 32K. */ char *dest, *src; memcpys(dest, src, strsz); /* casts strsz to short */ . . .
Some recent IBM processors include a hardware feature
called the Logical String Assist, which implements the C functions
strlen
,
strcpy
,
strcmp
,
memchr
, and
strchr
in hardware. To make use of this hardware feature,
#define
the symbol
_USELSA
before
including
<string.h>
or
<lcstring.h>
. The resulting code will not execute on hardware that
does not have the Logical String Assist feature installed.
Chapter Contents |
Previous |
Next |
Top of Page |
Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.