1. the memcpy function is used to copy the resource memory (memory area pointed to by Src) to the target memory (memory area pointed to by DEST). How many copies? There is a size variable control
The number of copied bytes;
Function prototype: void * memcpy (void * DEST, void * SRC, unsigned int count );
Usage: (1) objects of no matter what type can be copied. because the number of arguments of the function is void * (no type pointer is defined), that is to say, the passed real arguments can be int *, short *, char *, etc,
However, because the function copy process is a byte copy, the void * must be forcibly converted to char * in actual operations *, in this way, when the pointer is added, a byte is added each time.
Function source code implementation:
Void * memcpy1 (void * DESC, const void * SRC, size_t size)
{
If (DESC = NULL) & (src = NULL ))
{
Return NULL;
}
Unsigned char * desc1 = (unsigned char *) DESC;
Unsigned char * src1 = (unsigned char *) SRC;
While (size --> 0)
{
* Desc1 = * src1;
Desc1 ++;
Src1 ++;
}
Return DESC;
}
Int _ tmain (INT argc, _ tchar * argv [])
{
Int Dest [2] = {0 };
Const char SRC [5] = "1234 ";
// Printf (SRC );
Memcpy1 (DEST, SRC, sizeof (SRC ));
// * (DEST + 5) = '/0 ';
Printf (char *) DEST );
Int M =-1;
Return 0;
}
Note: (1) void * must return a value (pointer), which is not the same as void!
(2) First, we must infer that the pointer value cannot be blank. If DESC is empty, the memory space cannot be copied. If SRC is empty, it is equivalent to no copy. Therefore, return is;
(3) "" empty string indicates that the content is 0, null is 0, not a string; two are not equivalent;
(4) int Dest [2] = {0}; this is the method for initializing an array of the int type. If it is of the char type, char a [5] = "1234" is used "; note that the array subscript must be
More than the actual number of characters, because there are '/0'
(5) printf (char *) DEST); In this statement, the memory of the char type SRC uploaded to the int type DEST is forcibly converted to the char type and then printed out;
The Int-type DEST cannot be viewed directly, and the unsigned char * desc1 = (unsigned char *) DESC is available.
Stored in DeST. The memory size indicated by DEST is 4 bytes, which is forced to be converted into char, which means to divide the four bytes into one byte.
It is a string of characters. If it is defined as char Dest [5] = "1234", no conversion is required;
(6) memcpy1 (DEST, SRC, sizeof (SRC); pay attention to the sizeof (SRC) in it. This includes the string Terminator '/0; so don't worry about printf (DEST );
However, if memcpy1 (DEST, SRC, 4) is used, * (DEST + 5) = '/0' is required if'/0' is not used. This ensures that it is a complete string;
(7) Assume that during initialization:
Char Dest [1024] = "12345666"; // {0 };
Const char SRC [5] = "3333 ";
If memcpy1 (DEST, SRC, sizeof (SRC) is used for copying data, then printf (DEST );
Suppose memcpy1 (DEST, SRC, 4); then printf (DEST); the output is 33335666; because the above sizeof (SRC), including '/0 ', so copy the previous string to '/0'
The end is only 3333. If the character is 4 characters and '/0' is the fifth character, it will end with'/0' of Dest [1024], so it is 33335666.
Note the '/0' problem of the string !!!
Practical application:
Unsigned char g_pdata [1024] = "";
DWORD g_dwoffset = 0;
Bool packdatatoserver (const unsigned char * pdata, const unsigned int usize)
{
Memcpy (g_pdata + g_dwoffset, pdata, usize );
G_dwoffset + = usize;
// G_pdata + = usize;
Return true;
}
Void main ()
{
Const unsigned char a [4] = "123 ";
Packdatatoserver (A, 3 );
Packdatatoserver (A, 1111 );
Int B =-1;
}
The packdatatoserver () function is used to copy each resource memory to the target memory and accumulate the copy; that is, the next copy followed by the previous copy;
The memcpy function is obviously used;
The implementation principle is to use a global variable g_dwoffset to save the copy length before it is saved. At the beginning, we didn't think of this. The result is that each copy is one time, and the next copy
The last copy was cleared; therefore, the copy length was recorded using global variables;
The second thing to note is that during the copy process, do not change the direction of the target pointer, that is, the target pointer always points to the position at the time of initialization; so how can we implement the cumulative copy?
The pointer offset is used. During the first implementation, g_pdata + = usize is written into the function, so that the pointer displacement can be achieved, but the pointer orientation also changes;
In addition, g_pdata + = usize; the error "left operand must be L-value" is also reported because the address is assigned to an unchangeable pointer!
For example:
Char A [100];
Char * P = new char [10];
A = P; // an error occurs here. Note: The first address of the array is also a constant pointer, pointing to a fixed address and cannot be changed ~~
Char * const pp = new char [1];
Pp = A; // Error
Therefore, the first address cannot be changed, and the value must be accumulated (that is, the value must be assigned to the next memory block starting from the place where the value is assigned, and the pointer must be added ), so I want to write the pointer
In the number of records function, it is necessary to fully understand the implementation process of memcpy, which is a one-character value assignment. To assign values consecutively, we need to point the pointer to the first address of the continuous memory. Therefore,
It's really not easy to express. Well, that's it. It's just like that. It's a big piece of scattered knowledge...
Memcpy usage Summary