【本文连接】

http://www.cnblogs.com/hellogiser/p/strcpy_vs_memcpy.html

【分析】

strcpy和memcpy都是标准C库函数,它们有下面的特点。 strcpy提供了字符串的复制。即strcpy只用于字符串复制,并且它不仅复制字符串内容之外,还会复制字符串的结束符。

已知strcpy函数的原型是

 C++ Code 
1
 
char *strcpy(char *dest, const char *src);

memcpy提供了一般内存的复制。即memcpy对于需要复制的内容没有限制,因此用途更广。

 C++ Code 
1
 
void *memcpy( void *dest, const void *src, size_t count );

strcpy和memcpy主要有以下3方面的区别。
1、复制的内容不同。strcpy只能复制字符串,而memcpy可以复制任意内容,例如字符数组、整型、结构体、类等。
2、复制的方法不同。strcpy不需要指定长度,它遇到被复制字符的串结束符"\0"才结束,所以容易溢出。memcpy则是根据其第3个参数决定复制的长度。
3、用途不同。通常在复制字符串时用strcpy,而需要复制其他类型数据时则一般用memcpy

【代码】

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
using namespace std;

/*
 * copy strings by \0
 * (1) not check overlapping
 * (2) dest mush have enough space to contain the same C string as src including the \0
 * */
char *my_strcpy(char *dest, const char *src)
{
    if(src == NULL || dest == NULL)
        return NULL;
    char *d = dest;
    const char *s = src;
    while((*d++ = *s++) != '\0') // notice here
        ;
    return dest;
    /* support chaining
     * int len = strlen(my_strcpy(dest,src));
     * */
}

/*
 * not check overlapping
 * optimization: copy by word(4 or 8 bytes) instead of by 1 byte
 * */
void *my_memcpy(void *dest, const void *src, size_t count)
{
    if(src == NULL || dest == NULL)
        return NULL;
    char *d = (char *)dest;
    const char *s = (const char *)src;
    while(count--)
    {
        *d ++ = *s ++;
    }
    return dest;
}

/*
 * check overlapping
 * optimization: copy by word(4 or 8 bytes) instead of by 1 byte
 * */
/*
 * d == s
 * d <s, copying from the beginning
 * d >s, copying from the end
 * */
void *my_memmove(void *dest, const void *src, size_t count)
{
    if(src == NULL || dest == NULL)
        return NULL;
    char *d = (char *)dest;
    const char *s = (const char *)src;
    if(d < s)
    {
        //copy from the beginning
        while(count--)
        {
            *d++ = *s++;
        }
    }
    else if(d > s)
    {
        //copy from the end
;
        s = s + count - ;
        while(count--)
        {
            *d-- = *s--;
        }
    }
    else
    {
        // do nothing
    }
    return dest;
}

void test_case()
{
    char str1[] = "sample string";
    ];
    ];
    my_strcpy(str2, str1);
    my_strcpy(str3, "copy successful");
    printf("str1: %s\nstr2: %s\nstr3: %s\n", str1, str2, str3);
}

void test_case2()
{
    ];
    const char *src = "hello";
    my_memcpy(dest, src, strlen(src) + );
    printf("%s\n", dest);
}

int main()
{
    test_case();
    test_case2();
    ;
}

【参考】

http://www.cnblogs.com/stoneJin/archive/2011/09/16/2179248.html

http://www.cplusplus.com/reference/cstring/strcpy/

strcpy vs memcpy的更多相关文章

  1. strcpy和memcpy的区别(转载)

    strcpy和memcpy都是标准C库函数,它们有下面的特点.strcpy提供了字符串的复制.即strcpy只用于字符串复制,并且它不仅复制字符串内容之外,还会复制字符串的结束符. 已知strcpy函 ...

  2. 【转】 strcpy和memcpy的区别

    strcpy和memcpy都是标准C库函数,它们有下面的特点.strcpy提供了字符串的复制.即strcpy只用于字符串复制,并且它不仅复制字符串内容之外,还会复制字符串的结束符. 已知strcpy函 ...

  3. 关于strcpy和memcpy

    strcpy和memcpy都是标准C库函数,它们有下面的特点. strcpy提供了字符串的复制.即strcpy只用于字符串复制,并且它不仅复制字符串内容之外,还会复制字符串的结束符.已知strcpy函 ...

  4. strcpy和memcpy的区别

    strcpy和memcpy都是标准C库函数,它们有下面的特点.strcpy提供了字符串的复制.即strcpy只用于字符串复制,并且它不仅复制字符串内容之外,还会复制字符串的结束符. 已知strcpy函 ...

  5. strcpy与memcpy的区别

    strcpy和memcpy的区别 strcpy和memcpy都是标准C库函数,它们有下面的特点. strcpy提供了字符串的复制.即strcpy只用于字符串复制,并且它不仅复制字符串内容之外,还会复制 ...

  6. strcpy()、memcpy()、memmove()、memset()的内部实现

    一直想知道 strcpy().memcpy().memmove().memset()的内部实现 strcpy(), 字符串拷贝. char *strcpy(char *strDest, const c ...

  7. [置顶] strcpy和memcpy的区别

    strcpy和memcpy都是标准C库函数,它们有下面的特点. strcpy提供了字符串的复制.即strcpy只用于字符串复制,并且它不仅复制字符串内容,还会复制字符串的结束符. 已知strcpy函数 ...

  8. strcpy和memcpy

    切记,memcpy的头文件是memory.hstrcpy和memcpy主要有以下3方面的区别.1.复制的内容不同.strcpy只能复制字符串,而memcpy可以复制任意内容,例如字符数组.整型.结构体 ...

  9. sprintf、strcpy 及 memcpy 函数区别

    这些函数的区别在于 实现功能 以及 操作对象 不同.strcpy 函数操作的对象是 字符串 ,完成 从 源字符串 到 目的字符串 的 拷贝 功能. sprintf 函数操作的对象 不限于字符串 :虽然 ...

随机推荐

  1. Winform打包工具SetupFactory 9 的使用

    写了个WinForm的小程序..以前没打过包..只是直接把Bin里的东西复制出来使用..自己使用是足够.但是发给别人毕竟不太好看(不牛逼)..所以就想着打包.. Vs2012自带的有打包的功能..相信 ...

  2. Idea修改js和jsp不用重启

  3. 30秒搭建Github Page

    如果中国每个程序员都写博客,那么中国IT届的春天就来了 原文转自我的前端博客,链接:http://www.hacke2.cn/create-github-page/ 有同学问我的网站是怎么创建的,其实 ...

  4. [整理]AngularJS学习资源

    https://angular.io/docs/js/latest/(2.0官方网站) http://www.linuxidc.com/Linux/2014-05/102139.htm(Angular ...

  5. CSS文档流

    文档流 将窗体自上而下分成一行行, 并在每行中按从左至右的顺序排放元素,即为文档流. 每个非浮动块级元素都独占一行, 浮动元素则按规定浮在行的一端. 若当前行容不下, 则另起新行再浮动. 内联元素也不 ...

  6. 【AngularJS】—— 13 服务Service

    在AngularJS中有很多的服务,常用的比如$http,$location等等. 本篇文章会介绍一下的内容: 1 $http这种Angular提供的服务的使用 2 如何自定义服务,并总结服务需要注意 ...

  7. jstl c标签

    判断List是否为空的一种方法是使用jstl的c标签. <c:if test="${not empty cpInfo.cpCredentials}"> </c:i ...

  8. 微信电脑版微信1.1 for Windows更新 可@人/转发撤回消息/可播小视频

    微信电脑版微信1.1 for Windows发布更新了,版本号为1.1.0.18,群聊可@人/可转发撤回消息/可播小视频,功能越来越接近微信手机版了. 本次更新的一些新特点: 群聊中可以@人. 消息可 ...

  9. git ssh-add 报错 ssh-add Could not open a connection to your authentication agent

    $ ssh-add ~/.ssh/id_rsa.pub Could not open a connection to your authentication agent. 启动ssh-agent服务 ...

  10. 自定义Listview

    public class MyListView extends ListView { public MyListView(Context context) { super(context); } pu ...