strcpy vs memcpy

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   io   os   ar   for   

【本文串連】

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/

strcpy vs memcpy

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.