The following is the main implementation of several memory library functions commonly used (memcpy, Memmove, Memset, memcmp):memcpy functions and Memmove functions:
Same point:
Both of the functions are implemented from SRC copies count characters to Dest.
Different points:
1, memcpy function does not consider whether the memory is covered, that is, he is only responsible for the completion of the copy work, and the value of the copy after the correct or not, it is ignored.
2. The Memmove function considers the problem of memory cover: 1) when there is no coverage, the function and copy result are the same as the memcpy function;
2) When there is memory coverage, it is possible to ensure that the copy is correct.
3, memory without coverage and coverage of the situation as follows:
The code is implemented as follows
memcpy: A memory copy function that copies n bytes from the starting position of the memory address referred to by the source SRC to the starting position of the memory address referred to by the target dest.
char* my_memcpy (char* dst,const char* src,size_t count)
{
assert (DST! = NULL);
assert (src! = NULL);
assert (Count <= strlen (SRC) +1);
char* pDst = DST;
const char* PSRC = src;
While (count--)
{
*pdst = *psrc;
pdst++;
psrc++;
}
return DST;
}
--------------------------------------------------------------------------------------------------------------- -----------------------------
/* the memmove is used to copy count characters from Src to dest, and if the target region and source area overlap,memmove can guarantee the source string before it is overwritten
copies the bytes of the overlapping region to the target area. However, the SRC content will be changed after copying. However, when the target area does not overlap the source area, the function is the same as the memcpy function. */
char* my_memmove (char* dst,const char* src,size_t count)
{
assert (DST! = NULL);
assert (src! = NULL);
assert (Count <= strlen (SRC) + 1);
char* pDst = DST;
const char* PSRC = src;
if (pDst > PSRC + count | | pDst < PSRC)//memory overwrite not included
{
//forward copy
While (count--)
{
*pdst = *psrc;
pdst++;
psrc++;
}
}
else//There is a memory overwrite problem
{
//Reverse copy (from tail to head)
pDst + = count;
PSRC + = count;
while (count--)
{
*PDST = *PSRC;
pdst--;
psrc--;
}
}
return DST;
}
--------------------------------------------------------------------------------------------------------------- -------------------------------------------------
memset function: Sets the first count bytes of the memory area referred to as STR to character C. ( Note: This function is set to bytes )
Memset Memory Setting Function: Sets the first count bytes of the memory area referred to as STR to character C.
The code is implemented as follows :
void* My_memset (void* str, int c, int count)
{
ASSERT (str = NULL);
char* pStr = (char*) str;
while (count--)
{
*pstr = C;
pstr++;
}
return str;
}
1, Situation one: set the character array
int main ()
{
Char a[10];
memset (A, 0, 10);
}
The array A is a character type, the character type occupies memory size is 1Byte, and the Memset function is also assigned in bytes, so you output no problem.
2, Situation Two: Set up the Shaping array
int main ()
{
int b[10];
memset (b, 0, 10);
}
array b is an integer, and using Memset is assigned by byte, so that after the assignment, the value of each array element is actually 0x01010101, which is the decimal 16843009. Rather than the desired 1. 3, if using memset (a,1,20), is a point to the memory of 20 bytes to be assigned, each with a number of 1 to fill, into the binary, 1 is 00000001, accounting for a byte. an int type of 4 bytes, together is 0000 0001,0000 0001,0000 0001,0000 0001, converted into 16 binary is 0x01010101, is equal to 16843009, The assignment of an int element is completed.
--------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------- -------------
memcmp is the first count byte of the comparison memory area str1 and str2. The function is compared by byte.
int my_memcmp (const void *STR1, const void *STR2, size_t count)
{
if (str1 = = NULL && STR2 = = null)
{
return 0;
}
const char* PSTR1 = (const char*) str1;
const char* PSTR2 = (const char*) str2;
int res = 0;
For (PSTR1, pStr2; count > 0; ++pstr1, ++PSTR2,--count)
{
res = *pstr1-*pstr2;
if (res! = 0)
Break ;
}
return res;
}
Implementation of memory library functions