Introduction to memmove, memcpy, and memccpy and implementation of functions

Source: Internet
Author: User
Tags define function

Memmove, memcpy, and memccpy functions are memory copies from one buffer zone 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)

Header file: # 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 memory content in SRC to the memory address in DeST. Unlike strcpy (), memcpy () completely copies n Bytes and does not end with a string ending '/0'.
Return Value: returns the pointer to the DeST.

Header file: # include <string. h>
Define the 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 specified by Src to the address indicated by DeST. Unlike memcpy (), memccpy () immediately stops copying if a specific value (int c) is encountered in SRC.
Return Value: returns the next byte pointer pointing to the value of C in DeST. If the returned value is 0, the first n bytes in the memory indicated by Src do not contain the bytes whose value is C.

Header file: # include <string. h>
Define the function: void * memmove (void * DEST, const void * SRC, size_t N );
Function Description: memmove () is to move from one buffer zone to another.
Return Value: returns the pointer to dest.

When dest <= src-count or dest> = src + count, the above three functions will not produce overwriting problems, that is, the source data will not be changed.
If not, the source data is changed.

For example:
Char a [] = {'A', 'B '};
Char B [] = {'C', 'D', 'E', 'F', 'G', 'H '};
Memmove (a, B, sizeof (B ));
Or directly char * p = B + 2; memmove (p, B, sizeof (B ));
Output Data: The data output in B has been changed.
It is found that even if the space pointed to by array a is insufficient to store data, it can be moved successfully.
Cause | dest-src | <count

If you allocate enough space to these functions before using them, there will be no overwriting problem. That is to say, if the space allocated to the outside is insufficient to store the data to be copied, the source data may be overwritten and changed.

# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>

Void main (void)
{
Int I = 0;
Char a [9] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'h ', '/0 '};
Char p [2] = {'Q', 'w'}; // or char * p = a + 2;
Memmove (p, a, sizeof ());
Puts ();
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 );
}
Observe the output result.
Change memmove (p, a, sizeof (a); to memcpy (p, a, sizeof (a); or memccpy (p, a, 'E ', sizeof (a); then observe the output result.
It can be seen that when the destination storage space is insufficient, the source data will be overwritten and changed.
If the target bucket is allocated enough space, the overwriting problem will not occur.

 

 

Implementation of memcpy (), memmove (), and memset ()

Void * memcpy (void * pvTo, const void * pvFrom, size_t size)

Compared with strcpy, memcpy does not end when '/0' is encountered, but will definitely copy n Bytes.
Void * memcpy (void * pvTo, const void * pvFrom, size_t size) </p> <p >{</p> <p> assert (pvTo! = NULL) & (pvFrom! = NULL); // Use assertions </p> <p> byte * pbTo = (byte *) pvTo; // prevent pvTo address changes </p> <p> byte * pbFrom = (byte *) pvFrom; // prevent pvFrom changing the pvFrom address </p> <p> while (size --> 0) </p> <p> * pbTo ++ = * pbFrom ++; </p> <p> return pvTo; </p> <p>}

 

The memmove () function copies the first n Bytes pointed to by the pointer src to the first n MEMORY regions pointed to by the dest.
The dest and scr memory may overlap, and both of them are well processed. This is also the difference with mencpy ().

Vord * memmove (void * DEST, const void * SRC, size_t count) <br/>{< br/> void * ret = DEST; <br/> If (DEST <= SRC | DEST> = SRC + count) <br/> {<br/> while (count --) <br/> * DEST ++ = * SRC ++ <br/>}< br/> else <br/>{< br/> DEST + = count-1; <br/> SRC + = count-1; <br/> while (count --) <br/> * dest -- = * SRC -- <br/>}< br/> return ret; <br/>}

 

Void * memset (void * s, int c, int n)
Use c to fill the first n Bytes of the memory area pointed to by pointer s. The returned pointer s. s to the memory area is not necessarily a pointer to a character,
It is a pointer to any type, or even a pointer to a structure.

Void * memset (void * buffer, int C, int count) <br/>{< br/> char * pvto = (char *) buffer; <br/> assert (buffer! = NULL); <br/> while (count --> 0) <br/> * pvto ++ = (char) C; <br/> return buffer; <br/>}< br/>

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.