I. Differences between strcpy, memcpy and memset.
Strcpy
Prototype: extern char * strcpy (char * DEST, char * SRC );
Usage: # include <string. h>
Function: Copies the string ending with null indicated by Src to the array indicated by DeST.
Note: The memory areas specified by Src and DEST cannot overlap and DEST must have sufficient space to accommodate SRC strings.
Returns the pointer to DeST.
For example, char a [100], B [50]; strcpy (a, B); If strcpy (B, A) is used ), note whether the length of the string (before the first '/0') in a exceeds 50 bits. Otherwise, the memory address of B may overflow.
Memcpy
Prototype: extern void * memcpy (void * DEST, void * SRC, unsigned int count );
Usage: # include <string. h>
Function: copy count bytes from the memory area indicated by Src to the memory area indicated by DeST.
Note: the memory areas specified by Src and DEST cannot overlap. The function returns a pointer to DeST. You can copy any data type object.
For example, char a [100], B [50]; memcpy (B, A, sizeof (B); If sizeof (a) is used ), b's memory address overflows.
Memset
Prototype: extern void * memset (void * buffer, int C, int count );
Usage: # include <string. h>
Function: sets the first count byte of the memory area referred to by buffer to character C.
Note: The pointer to the buffer is returned. It is used to set all the memory space to a certain character.
Example: Char A [100]; memset (A, '/0', sizeof ());
Memset can easily clear a variable or array of the structure type.
For example:
Struct sample_struct
{
Char csname [16];
Int iseq;
Int itype;
};
For Variables
Struct sample_strcut sttest;
In general, the method of clearing sttest is as follows:
Sttest. csname [0] = '/0 ';
Sttest. iseq = 0;
Sttest. itype = 0;
Memset is very convenient:
Memset (& sttest, 0, sizeof (struct sample_struct ));
If it is an array:
Struct sample_struct test [10];
Then
Memset (test, 0, sizeof (struct sample_struct) * 10 );
If you have doubts about this problem, it is not about the function, but about the difference between MEM and Str.
Mem is a piece of memory. Its length must be remembered by yourself.
STR is also a piece of memory, but its length can be calculated at any time.
Therefore, memcpy requires the third parameter, while strcpy does not.
Ii. Differences between memcpy, memccpy, and memmove.
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.
Iii. Several implementations of memcpy:
1. Microsoft Edition
void * __cdecl memcpy (void * dst, const void * src, size_t count){ void * ret = dst; while (count--) { *(char *)dst = *(char *)src; dst = (char *)dst + 1; src = (char *)src + 1; } return(ret);}
2. interview answer Edition
Void * mymemcopy (void * DEST, const void * SRC, size_t count) {char * pdest = static_cast <char *> (DEST ); const char * psrc = static_cast <const char *> (SRC); // note that this is the key. Why do we need to compare it like this? Why? If (pdest> psrc & pdest <psrc + count) {for (size_t I = count-1; I <= 0; ++ I) {pdest [I] = psrc [I] ;}} else {for (size_t I = 0; I <count; ++ I) {pdest [I] = psrc [I] ;}} return pdest ;}3, Internet version
Void * my_memcpy (void * DST, void * SRC, int size)
{
Int Len = sizeof (long );
Long * pdst = (long *) DST;
Long * psrc = (long *) SRC;
For (; size> = Len; ++ pdst, ++ psrc, size-= Len)
{
* Pdst = * psrc;
}
For (LEN = size, size = 0; Size <Len; ++ size)
{
* (Char *) pdst + size) = * (char *) psrc + size );
}
Return DST;
}
Int main ()
{
Char SRC [] = "hello", DST [10] = {'/0 '};
My_memcpy (DST, SRC, 5 );
Printf ("DST = % s/n", DST );
Return 0;
}
4. If you have other versions, you are welcome to provide them.