strcpy的定義以及實(shí)現(xiàn)
1.cplusplus中對(duì)strcpy的解釋
Copies the C string pointed by source into the array pointed by destination, including the terminating null character (and stopping at that point).
2.函數(shù)的聲明
char * strcpy(char * destination,const char * source);
3.函數(shù)的實(shí)現(xiàn)
char * strcpy(char * destination, const char * source)
{
if (destination == NULL || source == NULL)
{
return NULL;
}
char *result = destination;
while((*destination++ = *source++) != '\0');
return result; //支持鏈?zhǔn)奖磉_(dá)式
}
從函數(shù)的實(shí)現(xiàn)可以看出,當(dāng)source字符串比destination字符串要長(zhǎng)時(shí),可能會(huì)溢出;當(dāng)遇到‘\0’時(shí),停止復(fù)制(‘\0’已經(jīng)被復(fù)制);當(dāng)source字符串與destination字符串復(fù)制會(huì)出錯(cuò)。
strncpy的定義以及實(shí)現(xiàn)
1.cplusplus中對(duì)strncpy的解釋
Copies the first num characters of source to destination. If the end of the source C string (which is signaled by a null-character) is found before num characters have been copied, destination is padded with zeros until a total of num characters have been written to it.
2.函數(shù)的聲明
char * strncpy(char * destination,const char * source,size_t num);
3.函數(shù)的實(shí)現(xiàn)
char * strncpy(char * destination,const char * source,size_t num)
{
if (destination == NULL || source == NULL)
{
return NULL;
}
char *result = destination;
while(num-- && (*destination++ = *source++) != '\0');
if(num != 0)
{
while(num--)
{
*destination++ = '\0';
}
}
return result;
}
從函數(shù)的實(shí)現(xiàn)看出,strncpy函數(shù)一定會(huì)將soure的字符串復(fù)制num個(gè)字節(jié)到destination中(如果字符不夠,則補(bǔ)'\0');但是如果source的字符串長(zhǎng)于num,那么destination字符串中就會(huì)缺少'\0'。
memcpy的定義以及實(shí)現(xiàn)
1.cplusplus中對(duì)memcpy的解釋
Copies the values of num bytes from the location pointed to by source directly to the memory block pointed to by destination.
翻譯一下,就是從源內(nèi)存地址的起始位置開(kāi)始拷貝若干個(gè)字節(jié)到目標(biāo)內(nèi)存地址中(因此在實(shí)現(xiàn)的時(shí)候需要將void 轉(zhuǎn)為char,實(shí)現(xiàn)按字節(jié)復(fù)制)。
2.函數(shù)的聲明
`void * memcpy(void * destination,const void * source,size_t num);·
3.函數(shù)的實(shí)現(xiàn)
void * memcpy(void * destination,const void * source,size_t num)
{
if(destination == NULL || source == NULL || num <= 0)
{
return NULL;
}
void * result = destination;
while (num--)
{
*(char *)destination++ = *(char *)source++;
}
return result;
}
由于函數(shù)memcpy是按內(nèi)存復(fù)制,所以可以對(duì)任何類(lèi)型進(jìn)行復(fù)制,但是在復(fù)制字符串的時(shí)候,如果source長(zhǎng)度比num長(zhǎng),那么不會(huì)添加'\0'。
以上三種方法,全部都無(wú)法解決內(nèi)存重疊的問(wèn)題。
memmove的定義以及實(shí)現(xiàn)
1.cplusplus中對(duì)memmove的解釋
Copies the values of num bytes from the location pointed by source to the memory block pointed by destination. Copying takes place as if an intermediate buffer were used, allowing the destination and source to overlap.
允許內(nèi)存重疊,在針對(duì)可能發(fā)生內(nèi)存重疊的情況則從后往前復(fù)制。
2.函數(shù)的聲明
void * memmove(void * destination,const void * source,size_t num);
3.函數(shù)的實(shí)現(xiàn)
void * memmove(void * destination,const void * source,size_t num)
{
if(destination == NULL || source == NULL || num <= 0)
{
return NULL;
}
char * result = destination;
const char * tmps = source;
if (tmps > result)
{
while (num--)
{
*result++ = *tmps++
}
}
else if (tmps < result)
{
result = result + num - 1;
tmps = tmps + num - 1;
while (num--)
*result-- = *tmps--;
}
return result;
}
在函數(shù)中,比較了源地址和目標(biāo)地址。當(dāng)源地址大于目標(biāo)地址的時(shí)候。直接復(fù)制;
當(dāng)源地址和目標(biāo)地址相等的時(shí)候,不復(fù)制;當(dāng)源地址小于目標(biāo)地址的時(shí)候,反向復(fù)制即可。
如有疑問(wèn)或錯(cuò)誤,請(qǐng)留言指出。