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 the 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 C in the source. 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.
Program running result:
This blog is reposted from Ghost.
There is a problem, in the above program, the relationship between the address of variable A and the address of Variable P in memory allocation, a in the high address, P in the low address or a in the low address, what about P in the high address space?
In this example, the address allocated to P is lower than the address allocated to a variable. That is to say, the space of Variable P is followed by the space of variable A. the test platform x86, ubuntu12.04, gcc/g ++ asked colleagues to try Variable Allocation Under vs2005 windowxp. The variable address allocated later should be lower than the variable address allocated first.
Is the allocation of space occupied by local variables in this function related to compiler settings? Hope you can give answers and guidance.
Test:
Char A [] = {'A', 'B '};
Char B [] = {'C', 'D', 'E', 'F '};
Memmove (a, B, sizeof (B ));
Puts ();
Puts ("");
Puts (B );
Printf ("A address is % u \ n", );
Printf ("B address is % u \ n", B );
Indeed, the source address data is overwritten. The address of variable B is greater than variable.
Please give answers.