The usage of C + + face question 4:memcpy

Source: Internet
Author: User


The usage of C + + face question 4:memcpy

The memcpy function is used to copy the resource memory (the area of memory pointed to by SRC) to the target memory (the area of memory pointed to by desk); How many copies? There is a size variable control
Usage:You can copy an object of any type, because the parameter type of the function isvoid*, which means that the arguments passed in can beint *,short*,char*Wait a minute.
Prototypes:void *memcpy(void *desc, void *src, unsigned int count)
Implement memcpy

void*memcpy (void*desc,Const void*src,size_t size) {//Illegal input    if(desc = =NULL|| src = =NULL)    {return 0; }//Because the function copy is a byte-by-byte copy, the type conversion    unsigned Char*DESC1 = (unsigned Char*) desc;unsigned Char*src1 = (unsigned Char*) src; while(size--)        {*desc1=*src1;        desc1++;    src1++; }returnDesc;} The inadequacy of this procedure lies in the fact that overlapping overlaps are not considered. If DESC&GT;SRC but Src+count>desc, we will have to pay attention to copy at this time. Exact code. CPPvoid*memcpy (void*desc,Const void*SRC, size_t size) {//Illegal input    if(desc = =NULL|| src = =NULL)    {return 0; }unsigned Char*DESC1 = (unsigned Char*) desc;unsigned Char*src1 = (unsigned Char*) src;if(Desc1 > Src1 && src1 + size > Desc) { for(size_t i = size-1; I >=0;        i--) {Desc1[i]=src1[i]; }    }Else{ for(size_t i =0; I < n;        i++) {Desc1[i]=src1[i]; }    }returnDesc;}

"Writing High-quality code: 150 recommendations for improving C + + programs" Recommendation 20: Use the memcpy () series function Be careful enough
These memory operation functions, such as memcpy (), memset (), memcmp (), often help us with some data copying, assignment, and so on. Because in the C language, whether it is a built-in type or a custom struct type (struct), its memory model is known and transparent to us. Therefore, we can operate on this object's underlying byte sequence, which is simple and effective.
In the traditional C-style data type is called the Pod object, namely an ancient pure data, the second binary content can be copied arbitrarily, so we can boldly use the function of memset (), memcpy (), memcmp () to manipulate the memory of the object.
But in C + +, be aware that C + + objects may not be a pod, which is why?
This is from the C + + dynamic polymorphism, the dynamic polymorphism of a basic support technology is virtual function, in the use of virtual functions, each time the class inheritance will produce a virtual function table (vtable), which holds pointers to virtual functions, these virtual function tables must be stored in the object body, That is, with the object's data is stored together, so the memory of the object data is not stored in a continuous manner, but is cut into different parts, since the object's data is no longer concentrated together, if this time using memcpy, will bring unpredictable consequences.
Summary: To distinguish which data objects are pods and which are non-pods. Because non-pod objects exist, the functions of the memcpy () series in C + + must be kept careful enough


The usage of C + + face question 4:memcpy

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.