(轉載)http://blog.sina.com.cn/s/blog_4d3a41f40100cvza.html
實現void *memcpy(void *to, const void *from, size_t count)這個函數。首先對這個函數作一些說明。html
#include <string.h>c++
void *memcpy(void *to, const void *from, size_t count)數組
函數memcpy()從from指向的數組向to指向的數組複製count個字符。若是兩數組重疊,不定義函數的行爲。函數
在C99中,to和from由restrict修飾。spa
先來看看微軟的實現:rest
void * __cdecl memcpy (
void * dst,
const void * src,
size_t count
)
{
void * ret = dst;htm
#if defined (_M_IA64)
{
extern void RtlMoveMemory( void *, const void *, size_t count );blog
RtlMoveMemory( dst, src, count );
}
#else
while (count--) {
*(char *)dst = *(char *)src;
dst = (char *)dst 1;
src = (char *)src 1;
}
#endif get
return(ret);
}string
再來看看coreutils中的實現:
void *
memcpy (void *destaddr, void const *srcaddr, size_t len)
{
char *dest = destaddr;
char const *src = srcaddr;
while (len-- > 0)
*dest++ = *src++ ;
return destaddr;
}
再來看Linux中實現:
void *memcpy(void *dest, const void *src, size_t count)
{
char *tmp = dest;
const char *s = src;
while (count--) *tmp++ = *s++ ; return dest;}