r****o 发帖数: 1950 | 1 我看到的标准strcpy()函数通常都是如下写法:
char *strcpy(char *strDest, const char *strSrc)
{
assert ((strDest!=NULL)&&(strSrc!=NULL));
char *strDest2=strDest;
while ((*strDest2++=*strSrc++)!='\0')
{
}
return strDest;
}
我看到的标准memcpy()函数通常都是如下写法:
void *memcpy(void *ptrDest, const void *ptrSrc, size_t size)
{
assert((ptrDest!=NULL)&&(ptrSrc!=NULL);
void *ptrDest2=ptrDest;
void *ptrSrc2=ptrSrc;
while (size-- >0)
*ptrDest2++=*ptrSrc2++;
return ptrDest;
}
我不明白的是为什么memcpy()函数里面的pt | u**s 发帖数: 50 | 2 1. I think saving "ptrSrc" in memcpy has no special purpose. Someone just
uses one extra local variable.
2. In real system, those codes (strcpy & memcpy & ...) usually will never
get chance to run because there are much faster arch-dependent assembly
versions for all those functions. Search memcpy/strcpy/memmove in linux
kernel tree/glibc you will find the real codes.
And, I roughly remember in the comments at the beginning of those codes
Linus wrote something like, "The following implementati
【在 r****o 的大作中提到】 : 我看到的标准strcpy()函数通常都是如下写法: : char *strcpy(char *strDest, const char *strSrc) : { : assert ((strDest!=NULL)&&(strSrc!=NULL)); : char *strDest2=strDest; : : while ((*strDest2++=*strSrc++)!='\0') : { : } : return strDest;
|
|