• std::memset、std::memcpy和std::strcpy的区别


    memset

    Fill block of memory

    <cstring>
    void * memset ( void * ptr, int value, size_t num );
    Sets the first num bytes of the block of memory pointed by ptr to the specified value (interpreted as an unsigned char).

    Parameters

    ptr
    Pointer to the block of memory to fill.
    value
    Value to be set. The value is passed as an int, but the function fills the block of memory using the unsigned char conversion of this value.
    num
    Number of bytes to be set to the value.

    Return Value

    ptr is returned.

    Example

     1 #include <iostream>
     2 #include <cstring>
     3 using namespace std;
     4 
     5 int main ()
     6 {
     7   char str[] = "almost every programmer should know memset!";
     8   memset (str,'-',6);
     9   cout<<str;
    10   return 0;
    11 }

    Output:

    1 ------ every programmer should know memset!

    memcpy

    Copy block of memory

    <cstring>
    void * memcpy ( void * destination, const void * source, size_t num );
    Copies the values of num bytes from the location pointed by source directly to the memory block pointed bydestination.

    The underlying type of the objects pointed by both the source and destination pointers are irrelevant for this function; The result is a binary copy of the data.
    The function does not check for any terminating null character in source - it always copies exactly num bytes.
    To avoid overflows, the size of the arrays pointed by both the destination and source parameters, shall be at leastnum bytes, and should not overlap (for overlapping memory blocks, memmove is a safer approach).

    Parameters

    destination
    Pointer to the destination array where the content is to be copied, type-casted to a pointer of type void*.
    source
    Pointer to the source of data to be copied, type-casted to a pointer of type void*.
    num
    Number of bytes to copy.

    Return Value

    destination is returned.

    Example

     1 #include <iostream>
     2 #include <cstring>
     3 using namespace std;
     4 
     5 int main ()
     6 {
     7   char str1[]="Sample string";
     8   char str2[40];
     9   char str3[40];
    10 
    11   memcpy (str2,str1,strlen(str1)+1);
    12   memcpy (str3,"copy successful",16);
    13 
    14   cout<<"str1:"<<str1<<endl;
    15   cout<<"str2:"<<str2<<endl;
    16   cout<<"str3:"<<str3<<endl;
    17 
    18   return 0;
    19 }

    output:

    1 str1: Sample string
    2 str2: Sample string
    3 str3: copy successful

    strcpy

    Copy characters from string

    <cstring>

    char * strcpy ( char * destination, const char * source );

    Copies the C string pointed by source into the array pointed by destination, including the terminating null character.To avoid overflows, the size of the array pointed by destination shall be long enough to contain the same C string as source (including the terminating null character), and should not overlap in memory with source.

    Parameters

    destination
    Pointer to the destination array where the content is to be copied.
    source
    C string to be copied.

    Return Value

    destination is returned.

    Example

     1 #include <iostream>
     2 #include <cstring>
     3 using namespace std;
     4 int main ()
     5 {
     6   char str1[]= "Sample string";
     7   char str2[40];
     8   char str3[40];
     9   strcpy (str2,str1);
    10   strcpy (str3,"copy successful");
    11   
    12   cout<<"str1:"<<str1<<endl;
    13   cout<<"str2:"<<str2<<endl;
    14   cout<<"str3:"<<str3<<endl;
    15 
    16   return 0;
    17 }

    Output:

    1 str1: Sample string
    2 str2: Sample string
    3 str3: copy successful
    **************************************************************
    我喜欢程序员,他们单纯、固执、容易体会到成就感;面对困难,能够不休不眠;面对压力,能够迎接挑战。他们也会感到困惑与傍徨,但每个程序员的心中都有一个比尔盖茨或是乔布斯的梦想,用智慧把属于自己的事业开创。其实我是一个程序员
    [=.=]
  • 相关阅读:
    19_05_01校内训练[划分]
    19_05_01校内训练[polygon]
    [Untiy]贪吃蛇大作战(四)——游戏主界面
    [Untiy]贪吃蛇大作战(三)——商店界面
    [Untiy]贪吃蛇大作战(二)——规则界面
    [Untiy]贪吃蛇大作战(一)——开始界面
    [C#]简单的理解委托和事件
    [C#]关于override和new在重写方法时的区别
    [C#]关于逆变与协变的基本概念和修饰符in与out的意义
    [剑指Offer]剪绳子
  • 原文地址:https://www.cnblogs.com/kevinGaoblog/p/2552796.html
Copyright © 2020-2023  润新知