zoukankan      html  css  js  c++  java
  • 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
    **************************************************************
    我喜欢程序员,他们单纯、固执、容易体会到成就感;面对困难,能够不休不眠;面对压力,能够迎接挑战。他们也会感到困惑与傍徨,但每个程序员的心中都有一个比尔盖茨或是乔布斯的梦想,用智慧把属于自己的事业开创。其实我是一个程序员
    [=.=]
  • 相关阅读:
    「SAP技术」SAP MM MB5M报表不显示特殊库存数据
    python-day3-条件判断与循环
    python-day2-运算符
    Mysql数据库意外崩溃导致表数据文件损坏无法启动的问题解决
    图书管理系统(Servlet+Jsp+Java+Mysql,附下载演示地址)
    求解最长递增子序列(LIS) | 动态规划(DP)+ 二分法
    HTML+CSS+JavaScript实现植物大战僵尸(附演示地址)
    面试官:如何在Integer类型的ArrayList中同时添加String、Character、Boolean等类型的数据? | Java反射高级应用
    面试官:手撕十大排序算法,你会几种?
    用x种方式求第n项斐波那契数,99%的人只会第一种
  • 原文地址:https://www.cnblogs.com/kevinGaoblog/p/2552796.html
Copyright © 2011-2022 走看看