ANSI-C requires that the memcpy() library function in strings.h return a destination pointer and handle and can handle a count of zero:
/* extract of string.h * extern void * memcpy(void *dest, const void * source, size_t count); extern void memcpy2(void *dest, const void * source, size_t count); /* this function does not return dest and assumes count > 0 */ /* extract of string.c */ void * memcpy(void *dest, const void *source, size_t count) { uchar *sd = dest; uchar *ss = source; while (count--) *sd++ = *ss++; return (dest); }
For a simpler, faster choice, use memcpy2() when the function does not have to return the destination or handle a count of zero (refer the following listing).
/* extract of string.c */ void memcpy2(void *dest, const void* source, size_t count) { /* this func does not return dest and assumes count > 0 */ do { *((uchar *)dest)++ = *((uchar*)source)++; } while(count--); }
Replacing calls to memcpy() with calls to memcpy2() saves runtime and code size.