[Turn]memmove, memcpy and memccpy

Source: Internet
Author: User
Tags define function

Original address: http://www.cppblog.com/kang/archive/2009/04/05/78984.html

Some minor changes were made on the basis of the original text ~

The three functions of Memmove, memcpy, and memccpy are copies of memory, copied from one buffer to another.
Memmove (void *dest,void*src,int count)
memcpy (void *dest,void *src,int count)
memccpy (Void*dest,void*src,int ch,int count)
Table header files: #include <string.h>
Define function: void *memcpy (void *dest, const void *SRC, size_t N)
Function Description: memcpy () is used to copy the first n bytes of the memory content referred to by SRC to the memory address referred to by dest. Unlike strcpy (), memcpy () will completely replicate N bytes and will not end up with a string ending
Return value: Returns a pointer to Dest
Table header files: #include <string.h>
Define function: void *memccpy (void *dest, const void *SRC, int c, size_t N);
Function Description: memccpy () is used to copy the first n bytes of the memory content referred to by SRC to the address referred to by dest. Unlike memcpy (), memccpy () stops copying immediately if it encounters a specific value (int c) in Src.
Return value: Returns a pointer to the next byte in dest with a value of C. A return value of 0 indicates that there are no bytes of value C in the first n bytes of the memory referred to in SRC.
Table header files: #include <string.h>
Define function: void *memmove (void *dest, const void *SRC, size_t n);
Function Description: Memmove () is moved from one buffer to another buffer. The memory area referred to by SRC refers to the memory area where count bytes are copied to dest.
Return value: Returns a pointer to the dest.
When Dest <= src-count or dest >= Src+count, none of the above three functions will have an overwrite problem, i.e. the source data will not be changed.
If it is not in the range above, the source data will be changed.

Such as:
Char a[]={' A ', ' B '};
Char b[]={' C ', ' d ', ' e ', ' f ', ' g ', ' H '};
Memmove (a,b,sizeof (b));
or direct char *p=b+2;memmove (p,b,sizeof (b));
The output data will find that the data output in B has been changed.
It is found that even if the a array points to a space that is not enough to store data, it can be moved successfully.
Reason |dest-src |<count
If you use these functions, allocate enough space and then use them without overwriting problems. That is, if the externally allocated space is not sufficient to store the data to be copied, it is possible that the source data is overwritten by the change.

Code Validation

#include "stdafx.h" #include "string.h" #include "stdio.h" int main (int argc, char* argv[]) {    int i=0; char a[9]={' a ', ' B ', ' C ', ' d ', ' e ', ' f ', ' g ', ' h ', ' n '}; Char p[3]={' q ', ' W ', '};//' or char *p=a+2; printf ("%d,%d\n", a,p); printf ("Pre-overwrite: \ n"); puts (a); printf ("_________________ ____________________________\n "); Puts (p); printf ("_____________________________________________\n"); Memmove (P,a,sizeof (a)); printf ("post-overwrite: \ n"); Puts (a); printf ("_____________________________________________\n"); Puts (p); printf ("_____________________________________________\n"); for (i =0;i<10;i++) printf ("%c%d \ n", * (A+i), a+i); printf ("_____________________________________________\n"); for (i =0;i<8;i++) printf ("%c%d \ n", * (P+i), p+i);    return 0; }

  

Observe the results of the output.
Replace Memmove (P,a,sizeof (a)) with memcpy (P,a,sizeof (a)), or memccpy (p,a, ' e ', sizeof (a)), and then observe the results of the output.
It can be seen that when the destination storage space is low, there is a problem that the source data is overwritten.
If the destination storage space allocates enough space, the overwrite problem does not occur.

Replace Memmove (P,a,sizeof (a)) with memcpy (P,a,sizeof (a)), or memccpy (p,a, ' e ', sizeof (a)), and then observe the results of the output.
It can be seen that when the destination storage space is low, there is a problem that the source data is overwritten.
If the destination storage space allocates enough space, the overwrite problem does not occur.

[Turn]memmove, memcpy and memccpy

Related Article

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.