MEM Series functions are often in the interview when the knowledge points, we need to master the principles of these three functions and code implementation, to be able to accurately write the code.
memcpy, Memset, and memset three functions need to be included with the bottom file during use:
In C
#include <string.h>
In C + +
#include <cstring>
memcpy
The memcpy function is the memory copy function in C/s + +, which starts at the beginning of the memory address indicated by source SRC, and copies n bytes to the starting position of the memory address referred to by the destination DST.
The best way to study function function is to study its source code, which is found on the Internet, as follows:
void*__cdeclmemcpy (void* dst,constvoid* src,size_tcount)
{
void* ret = DST;
while (count--)
{
Note that the memcpy function does not handle the issue of whether DST and SRC regions overlap
* (char*) DST = * (char*) src;
DST = (char*) DST +1;
src = (char*) src +1;
}
return (ret);
}
The source code is simpler, defines a count, and then copies the values pointed to by SRC to DST from beginning to end, and the memcpy in the library function cannot handle the overlapping parts of DST and Src.
So if you're dealing with overlapping parts, we can copy from the back to the next, and here's the function code I modified:
void*__cdeclmemcpy (void* dst,constvoid* src,size_tcount)
{
CHAR*PDST =static_cast<char*> DST;
CONSTCHAR*PSRC =static_cast<constchar*> src;
Check parameters
if (pdst==null| | psrc==null| | Count <=0) {
Returnnull;
}
To determine whether there are overlapping parts
if (Pdst > Psrc && pdst < PSRC + count) {
for (size_ti=count-1; i>=0; i--)
{
Pdest[i] = Psrc[i];
}
}
else{
for (size_ti=0; i<count; i++)
{
Pdest[i] = Psrc[i];
}
}
RETURNPDST;
}
Memset
Memset is typically used to initialize memory, where it is worth noting that the Memset function is a value set to C for each byte (in bytes) of memory. The function prototype is as follows:
Voidmemset (VOID*S,INTC,SIZE_TN)
{
Constunsignedcharuc = c;//converts int to char, truncates high 24 bits of C, leaving low 8 digits
UNSIGNEDCHAR*SU;
for (su = s;0< n; ++su,--n)
*SU = UC;
Returns
}
Note that there is a pit, memset is generally used to clear the memory, and if you want to initialize this memory to 1, write down the following code:
INTNUM[10];
memset (num,1,sizeof (int) *10);
This is not going to be what you want, every number of NUM is initialized to 16843009, because the above mentioned will intercept C's high 24 bits.
Using memset initialization is much faster than initializing a for loop, so when initializing basic type data, structure, etc., choose Memset,memset to easily empty a variable or array of a struct type.
Memmove
It is similar to the function of memcpy, which replicates the N bytes of Src to the starting position of the memory address that DST refers to, and it handles the overlap between src and DST. However, when the target area does not overlap with the source area, the memcpy function functions the same. (memcpy basically consistent with the above modification)
So the basic principle is that if you can make sure that there are no overlapping portions of the memory, use memcpy to copy them, and if you are unsure, you must use Memmove to ensure the correctness of the copy.
The implementation code is as follows:
Void*memmove (void* dest,void* src,size_tcount)
{
void* ret = dest;
if (dest <= src | | | dest >= (src + count))
{
Non-overlapping buffers
Copy from lower addresses to higher addresses
while (count--)
*dest++ = *src++;
}
Else
{
Overlapping buffers
Copy from higher addresses to lower addresses
Dest + = count-1;
src = count-1;
while (count--)
*dest--= *src--;
}
Returnret;
}
strcpy
strcpy is a standard library function in C, which needs to be included with the strcpy file:
#include <string.h>
#include <stdio.h>
The function function is to copy the string containing the null terminator starting from the SRC address to the address space at the beginning of the DST, and return a pointer to DST. The function code is as follows:
char*strcpy (char* DST, char* src) {
if (dst==null| | Src==null) returnnull;//--1
if (DST==SRC) returndst;//--2
char* address = dst;//--3
while ((*dst++ = *src++)!= ' ")//--4
Returnaddress;//--5
}
The pictures are all the test centers, the following one by one explains:
1, the need to determine the correctness of parameters, here can also throw an exception
2, if pointed to the same piece of memory, do not copy directly back to
3, here you need to save the original DST pointer, as a return value
4, here is a skill, if written below two kinds, the interview will be greatly deducted points!
First Kind
while (*dst++ = *src++)//Direct cross-border access, no check the validity of the pointer
Second Kind
while (*src!= ' ") {*dst++ = *src++;} Considering the boundary problem of SRC, not adding ' a ' to the back of DST will cause the DST length to be unknown and cause errors
5, the function returns the original DST value is to be able to support the chain expression, increased the function of the additive.
The 5th above can be illustrated with the following test code:
Intlength =strlen (strcpy (STRA,STRB)); If a chained expression is not supported, an error will be present here.
Then sometimes you also ask why you do not return the original SRC value, the reason for the error is the following three points:
The source string is already known and returns no meaning
Expressions such as char * stra = strcpy (new CHAR[10],STRB) cannot be supported
To protect the source string, use const to qualify the contents of SRC, the const char as the return value of char, the type does not match, the compiler will complain.
The difference between strcpy and memcpy.
This is also a common test center, mainly divided into the following three points different:
Replication is different: strcpy can only copy strings, and memcpy could copy anything, such as character arrays, integers, structs, and so on
copied methods: strcpy does not need to specify the length, it encounters the end of the string Terminator ' ", so it is easy to overflow. memcpy you need to pass in the third parameter to specify the length
different: usually use strcpy when copying strings, and memcpy when you need to copy other data types.