【本文连接】

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
        d = d + count - 1;
        s = s + count - 1;
        while(count--)
        {
            *d-- = *s--;
        }
    }
    else
    {
        // do nothing
    }
    return dest;
}

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

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

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

【参考】

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

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

转载于:https://www.cnblogs.com/hellogiser/p/strcpy_vs_memcpy.html

strcpy vs memcpy相关推荐

  1. strcpy和memcpy的区别(转)

    转自:http://www.cnblogs.com/stoneJin/archive/2011/09/16/2179248.html strcpy和memcpy都是标准C库函数,它们有下面的特点. s ...

  2. C/C++基础面试集锦(一)strcpy、memcpy

    **一.strcpy函数实现方法** char* strcpy(char *dest, const char *src) {assert((dest != NULL) && (src ...

  3. strcpy()和memcpy()的区别

    Strcpy和memcpy都可以用来拷贝字符串,strcpy以\0结束,但memcpy必须指定拷贝的长度,类似于strncpy Strncpy与memcpy类似,区别是:当遇到\0时,strncpy停 ...

  4. 【c++】10. memset()、【strcpy_s()、memcpy_s()】、【strcpy(),memcpy()】

    选择使用[strcpy_s.memcpy_s]还是选择[strcpy,memcpy]? memset()的用法 memcpy_s,strcpy_s函数明确的指定了目标内存的大小,能够清晰的暴露出内存溢 ...

  5. strcpy和memcpy的区别 | strcpy和strncpy的区别

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

  6. c语言深入浅出(一)strcpy和memcpy的区别

    c语言深入浅出(一)strcpy和memcpy的区别 strcpy和memcpy都是c语言的库函数 strcpy:只用于字符串的复制,当碰到'\0'就停止了 memcpy:用于这个内存的拷贝,适用于结 ...

  7. strcpy、memcpy和memset的区别

    strcpy 原型:extern char *strcpy(char *dest,char *src); 用法:#include <string.h> 功能:把src所指由NULL结束的字 ...

  8. C语言中的复制函数(strcpy和memcpy)

    strcpy和strncpy函数 这个不陌生,大一学C语言讲过,其一般形式为strcpy(字符数组1,字符串2)作用是将字符串2复制到字符数组1中去. EX: char str1[10]='',str ...

  9. 库函数strcpy、memcpy和memset

    编写一个函数,作用是把一个char组成的字符串循环右移n个. void LoopMove(char pStr, int steps) { int n = strlen(pStr) - steps; c ...

最新文章

  1. 特征工程系列:特征筛选的原理与实现(下)
  2. SAP MM Overall Level 审批的采购申请中行项目里的成本中心必须是同一个!
  3. 将EXE安装包重新封装成MSI格式
  4. php 数组存入mysql_PHP将数组存入数据库中的四种方式
  5. [Java基础]并发修改异常
  6. 基于Alluxio系统的Spark DataFrame高效存储管理技术
  7. 【Java】计算1+3+5+7+9和100以内的素数
  8. SLAM Cartographer(14)Global SLAM的主线业务
  9. java工程师面试宝典_【Java工程师面试宝典】学习说明_互联网校招面试真题面经汇总_牛客网...
  10. Ubuntu16.04下部署 nginx+uwsgi+django1.9.7(虚拟环境pyenv+virtualenv)
  11. 西南交大数据结构c语言版集合的并,交实验报告答案,数据结构(C语言版)实验报告 集合的交并差...
  12. GBK和UNICODE
  13. 蓝牙音箱连接成功但没有声音还是电脑的声音
  14. FFMPEG解码H264成YUV
  15. python列表的事例_python列表使用实例
  16. ubuntu批量压缩文件夹
  17. java小组口号,小组口号霸气
  18. Codeforces 272C Dima and Staircase 思维 or 线段树
  19. Unity Shader UV动画之高光材质加上透明材质与UV动画
  20. 【基础篇】MySQL系列之where条件查询

热门文章

  1. 获取和使用某些网站的iconfont图标字体
  2. Mybatis 动态表名,插入数据
  3. You must configure either the server or JDBC driver (via the 'serverTimezone' configuration property
  4. MySQL截取字符串的方法-substring_index
  5. 字符串%百分号 和 format 格式化
  6. Nginx+PHP-FPM优化技巧总结(转发别人的,自己留着收藏个记录用)
  7. NPM酷库:uuid,生成随机ID
  8. 搜索关键字高亮显示,就比微信多个多音字搜索
  9. XJTUOJ wmq的队伍(树状数组求 K 元逆序对)
  10. 巴士电台开放接口API