ANSI-C requires that the memcpy() library function in strings.h returns a pointer of the destination and handles and is able to also 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); }
If the function does not have to return the destination and it has to handle a count of zero, the memcpy2() function in the following listing is much simpler and faster:
/* 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.