strcpy,strncpy,memcpy以及memmove的比较以及实现

strcpy的定义以及实现

1.cplusplus中对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.函数的声明
char * strcpy(char * destination,const char * source);
3.函数的实现

char * strcpy(char * destination, const char * source)
{
    if (destination == NULL || source == NULL)
    {
        return NULL;
    }
    char *result = destination;
    while((*destination++ = *source++) != '\0');
    return result;     //支持链式表达式
}

从函数的实现可以看出,当source字符串比destination字符串要长时,可能会溢出;当遇到‘\0’时,停止复制(‘\0’已经被复制);当source字符串与destination字符串复制会出错。

strncpy的定义以及实现

1.cplusplus中对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.函数的声明
char * strncpy(char * destination,const char * source,size_t num);
3.函数的实现

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;
}

从函数的实现看出,strncpy函数一定会将soure的字符串复制num个字节到destination中(如果字符不够,则补'\0');但是如果source的字符串长于num,那么destination字符串中就会缺少'\0'。

memcpy的定义以及实现

1.cplusplus中对memcpy的解释

Copies the values of num bytes from the location pointed to by source directly to the memory block pointed to by destination.

翻译一下,就是从源内存地址的起始位置开始拷贝若干个字节到目标内存地址中(因此在实现的时候需要将void 转为char,实现按字节复制)。
2.函数的声明
`void * memcpy(void * destination,const void * source,size_t num);·

3.函数的实现

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;
}

由于函数memcpy是按内存复制,所以可以对任何类型进行复制,但是在复制字符串的时候,如果source长度比num长,那么不会添加'\0'。
以上三种方法,全部都无法解决内存重叠的问题。

memmove的定义以及实现

1.cplusplus中对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.

允许内存重叠,在针对可能发生内存重叠的情况则从后往前复制。
2.函数的声明
void * memmove(void * destination,const void * source,size_t num);
3.函数的实现

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;
}

在函数中,比较了源地址和目标地址。当源地址大于目标地址的时候。直接复制;
当源地址和目标地址相等的时候,不复制;当源地址小于目标地址的时候,反向复制即可。

如有疑问或错误,请留言指出。

你可能感兴趣的:(strcpy,strncpy,memcpy以及memmove的比较以及实现)