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

Source: Internet
Author: User

From Baidu: http://wenku.baidu.com/view/804a4df24693daef5ef73de4.html ###


For more information, see .....


Memmove,MemcpyAndMemccpyIntroduction

Memmove , Memcpy And Memccpy All three 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 functions : Void * memcpy (void * DEST, const void * SRC, size_t N)

Function Description : Memcpy () Used for copying SRC Before the memory content N Bytes Dest Memory Address. And Strcpy () The difference is that , Memcpy () Will be completely copied N Bytes , It will not end with a string. '\ 0' End

Return Value : Back to Point Dest Pointer

Header file : # Include <string. h>

Define functions : Void * memccpy (void * DEST, const void * SRC, int C, size_t N );

Function Description : Memccpy () Used for copying SRC Before the memory content N Bytes Dest Address. And Memcpy () The difference is that , Memccpy () If SRC A specific value (Int
C) Stop copying immediately.
Return Value : Back to Point Dest The value is C The next byte pointer. The returned value is 0 Indicates that SRC Before memory N No value in bytes is C .

Header file : # Include <string. h>

Define functions : Void * memmove (void * DEST, const void * SRC, size_t N );

Function Description : Memmove () Is to move from one buffer zone to another buffer zone.  

Return Value : Back to Point Dest Pointer.

When DeST <= Src-count
Or DeST> = SRC + count The above three functions will not cause the overwriting problem, 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 will find B The data output has been changed.

Even if A The space pointed to by the array is insufficient for data storage and 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 (a); 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 );}

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.

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.